How to replace occurrences of "-" with an empty string?

Related searches

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("-","");

How to replace all occurrences of a string in JavaScript, Find out the proper way to replace all occurrences of a string in plain JavaScript, from regex to Duration: 2:12 Posted: Jul 2, 2018 To replace all occurrences of a string in JavaScript, we have to use regular expressions with string replace method. We need to pass a regular expression as a parameter with ‘g’ flag (global) instead of a normal string. In the above example, we will pass the parameter as regular expression as shown below

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.

JavaScript String replace() Method, Note: If you are replacing a value (and not a regular expression), only the first instance of the value will be replaced. To replace all occurrences of a specified� C Program to Replace All Occurrence of a Character in a String Example 3. This program to replace all character occurrences is the same as the first example. However, this time, we used the Functions concept to separate the logic.

String.Replace Method (String, String)

in your case it would be

string str = "123-456-7";
string tempstr = str.Replace("-","");

How to Replace All Occurrences of a String in JavaScript, You can use the JavaScript replace() method in combination with the regular expression to find and replace all occurrences of a word or substring inside any� You should note the following points while using the string.replace () method: If the count parameter is not specified, then all occurrences of the old string will get replaced with the new one. It there is some value in the count parameter, then the old string will get replaced specified no. of time by the new one.

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…
  • Correct! Sorry. I did had a wrong picture. Thanks for correcting me.