I want to replace about 8 characters in a string.
Would it be more efficient to use a Regex method or just use multiple calls to string.Replace()
I'm replacing about 7 characters that may appear, all to be underscores instead. The characters could appear anywhere in the string, and not in a particular order etc.
replace in JavaScript. To use RegEx, the first argument of replace will be replaced with regex syntax, for example /regex/ . This syntax serves as a pattern where any parts of the string that match it will be replaced with the new substring. The string 3foobar4 matches the regex /\d.
The REGEXREPLACE( ) function uses a regular expression to find matching patterns in data, and replaces any matching values with a new string. standardizes spacing in character data by replacing one or more spaces between text characters with a single space.
If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")
Don't use the Regex class unless you actually need to match a regular expression. It's much more efficient to let the String type do straight text or character matching if that's all your doing than to create a Regex.
The Regex class is much more powerful than simple character or string matching. This power does not come for free. Using a full regex to match a character / string is overkill. It's the equivalent of using a high power explosive to remove a single ant from your lawn when your shoe would do just fine.
You don't need to make multiple calls to string.Replace()
- it replaces all occurences in a single pass. See the MSDN documentation. You only need to make multiple calls if you are replacing different input sequences within the string (which may be what you're implying in your question).
In that case, I would use string.Split
and string.Join
for this:
var replaced = string.Join( "_", input.Split( new[]{'x','y','z'} ) );
This will split the string at every location where one of the characters 'x', 'y', or 'z' occurs (replace with your set) and will rejoin the fragments using the '_' character -- effectively replacing the originals. This approach is NOT necessarily more efficient than multiple calls to string.Replace
- that would depend on the length of the input string, the number of occurences of the characters to replace, and so on. You would need to do profiling with real-world data to determine what is faster. What this approach does do, however, is make the code more concise.
As far as performance is concerned - I would go with the simplest and most readable solution first, and if testing demonstrates a problem - then I would profile and decide what alternative solutions to pursue (if any). Unless there are strong reasons to do otherwise, my personal priorities when writing code are:
At the same time, depending on how long these strings may be and how many characters may need to be replaced, you should use a StringBuilder object rather than a string.
Strings are immutable, so in each replacement you'd be creating a new string with the underscore. The StringBuilder class is more efficient for multiple changes to a string object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With