Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Removing Characters from String

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the best way to remove unwanted characters from a string?

* So if the user enters String A, String B is created with unwanted characters removed from A.


String A = "Hello. How Are You?";
THEN
String B = "Hello How Are You";


I have tried something like this:



But I get an lang string array error for some reason.
 
Conrad McLaughlin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I went on a website and looked at replaceAll method. But when I compile this why is String A is exactly the same?

 
Conrad McLaughlin
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I went on a website and looked at replaceAll method. But when I compile this why is String A is exactly the same?

 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable. You need to capture the output of the method.
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Maybe this methods help you to understand the logic.

 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by memati bas:
Hello,
Maybe this methods help you to understand the logic.




Oh soory for the wrong code. The developed codes is at the below.

 
lowercase baba
Posts: 12893
63
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To state it a little more explicitly, once a string is created, it CANNOT be changed. So what good are all those "replaceAll()" type methods???

simple. they RETURN a NEW string. so when you call

A.replaceAll(".","");

string A is not changed,, but a brand new string is created where the "." have been replaced with "". you need some new variable, say "String B", and then write this:

String B = A.replaceAll(".","");

A will still contain the original string, and B will be the string you want.
 
My pie came with a little toothpic holding up this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic