How to replace occurrences of "-" with an empty string?
I have this string: "123-456-7"
I need to get this string: "1234567"
How I can replace occurrences of "-" with an empty string?
string r = "123-456-7"; r = r.Replace("-", "");
How to replace all occurrences of a string?, Note: Don't use this in performance critical code. As an alternative to regular expressions for a simple literal string, you could use str = "Test abc test test abc test. One of the several ways to find all occurrences and then replace the searched term is by using a combination of find and sed commands. You can easily do so with a single command: In this example, you have 50 web pages in the pages folder.
This should do the trick:
String st = "123-456-7".Replace("-","");
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 String.Empty is faster than "".
Also String.Empty mean no typo errors.
Check the What is the difference between String.Empty and "" link.
replacingOccurrences(of:with:), The string with which to replace target . Return Value. A new string in which all occurrences of target in the receiver are replaced by replacement . Enter a String :Sachin Tendulkar /* Replace a to @ */ After Replace: [email protected] [email protected] Example: How to Replace all occurrences of a character in string in C++.
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.
in your case it would be
string str = "123-456-7"; string tempstr = str.Replace("-","");
The primitive approach to replace all occurrences is to split the string into chunks by the search string, the join back the string placing the replace string between chunks: string.split (search).join (replaceWith). This approach works, but it’s hacky.
Click the Replace button in the Editing section of the ribbon. Type the word to replace into the Find what field. Type the replacement word to use into the Replace with field, then click the Replace All button. The problem you may run into with the feature is that it is replacing that text string, not just occurrences of the word.
I have this string: "Test abc test test abc test test test abc test test abc" Doing: str = str.replace('abc', ''); seems to only remove the first occurrence of abc in the string above.
- Be careful, it won't work if you use single quotes (indicating a char) =>
r.Replace('-', '');That just bit me in the A**
- I do not believe this is true. String.Empty is the constant for "". The compiler points all "" literals to String.Empty. Doesn't matter how many "" literals you have.
- Thanks AMissico ... I just checked the heap for 3.5 framework and you are right, both "" and String.Empty point to the same location. But, for 1.0, there will be space allocated on the heap for "". I've edited the answer accordingly :)
- Just not true, see stackoverflow.com/questions/263191/…
- Correct! Sorry. I did had a wrong picture. Thanks for correcting me.