I have used replace function but it replaces '$' sign with space but i dont need space. I want to only remove '$' sign from mystring.
-
1Just google `how to delete the first char of a string`. – herohuyongtao Feb 22 '14 at 07:34
-
Can we see code you used? It will help use explain your mistake and provide correct solution. – Pshemo Feb 22 '14 at 07:39
6 Answers
After replacing $ from your string use
string.trim();
to remove spaces from string

- 808
- 1
- 9
- 22
Replace it with an empty string, instead of a space:
string.replace("$", "");
Or you might use substring:
string.substring(1);

- 32,893
- 7
- 60
- 87
String s="$49monthly";
String newstring=s.replace("$", "");
System.out.println("Value: "+newstring);

- 1,572
- 3
- 19
- 32
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

- 12,760
- 2
- 32
- 53
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. :)

- 87
- 7
-
1Instead 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
-
1Just 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