Hi
I have this string: "123-456-7"
I need to get this string: "1234567"
How I can replace occurrences of "-" with an empty string?
Thanks in advance
Hi
I have this string: "123-456-7"
I need to get this string: "1234567"
How I can replace occurrences of "-" with an empty string?
Thanks in advance
This should do the trick:
String st = "123-456-7".Replace("-","");
To be clear, you want to replace each hyphen (-) with blank/nothing. If you replaced it with backspace, it would erase the character before it!
That would lead to: 123-456-7 ==> 12457
Sean Bright has the right answer.
String.Replace Method (String, String)
in your case it would be
string str = "123-456-7";
string tempstr = str.Replace("-","\b");
StringBuilder strB = new StringBuilder("123-456-7"); strB.Replace("-", string.Empty); string result = strB.ToString();
string r = "123-456-7".Replace("-", String.Empty);
For .Net 1.0 String.Empty will not take additional space on the heap but "" requires storage on the heap and its address on the stack resulting in more assembly code. Hence Sting.Empty is faster than "".
Also String.Empty mean no typo errors.
Check the What is the difference between String.Empty and “” link.
Any of the above method I guess is fine whereas if you are into some complex operation better think of regex it is really great.