• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Removing Characters from String

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic