0

I have used replace function but it replaces '$' sign with space but i dont need space. I want to only remove '$' sign from mystring.

user3274718
  • 3
  • 1
  • 4

6 Answers6

1

After replacing $ from your string use

string.trim(); 

to remove spaces from string

swapnil7
  • 808
  • 1
  • 9
  • 22
1

Replace it with an empty string, instead of a space:

string.replace("$", "");

Or you might use substring:

string.substring(1);
femtoRgon
  • 32,893
  • 7
  • 60
  • 87
1

Use the following:

string.replace("$","");
nikis
  • 11,166
  • 2
  • 35
  • 45
1
String s="$49monthly";
String newstring=s.replace("$", "");
System.out.println("Value: "+newstring);
Ajeesh
  • 1,572
  • 3
  • 19
  • 32
0

You have to use String.replace() and then String.trim() methods, if you have spaces in the beginning and trailing ends of your String

        String test = "$stackoverflow";
        test = test.replace("$","");
        test = test.trim();
        System.out.println(test);

You need to assign the String back after manipulation since String literals are immutable in Java.

Output

stackoverflow

There will not be any spaces in the front and end, if you use String.trim() method

Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
0

I am not Java programmer but I have these links that might help you.

http://alvinalexander.com/blog/post/java/remove-non-alphanumeric-characters-java-string

http://www.rgagnon.com/javadetails/java-0030.html

Also make sure to check : http://www.youtube.com/watch?v=QgtmGpxy6zY

http://www.tutorialspoint.com/java/java_strings.htm Tutorialspoint can be useful to you if you are just beginner studying Java.

Sorry I can't write any code to show you, but be sure to study these links, you might learn something new. :)

  • 1
    Instead of providing a bunch of links, I suggest you try finding the relevant information in those links and then post an answer to the question here. – takendarkk Feb 22 '14 at 07:52
  • @Takendarkk On these links he can learn more about it and maybe learn something he didn't know, and if he doesn't want to learn something new he can view other answer from above. When learning something I didn't know I like to explore more on that topic so I wouldn't have any problems with it later on. That is what these links are for. – user314159265 Feb 22 '14 at 07:57
  • 1
    Just to clarify, I'm not suggesting that these links are bad in any way, just that if you aren't going to provide a specific answer you might want to consider posting these links as a comment, not an answer. – takendarkk Feb 22 '14 at 07:59
  • 1
    @Takendarkk In future, when I don't answer with code, links will be provided in comment. :) – user314159265 Feb 22 '14 at 08:02