I've written a class for processing strings and I have the following problem: the string passed in can come with spaces at the beginning and at the end of the string.
I need to trim the spaces from the strings and convert them to lower c开发者_运维技巧ase letters. My code so far:
var searchStr = wordToSearchReplacemntsFor.ToLower();
searchStr = searchStr.Trim();
I couldn't find any function to help me in StringBuilder
. The problem is that this class is supposed to process a lot of strings as quickly as possible. So I don't want to be creating 2 new strings for each string the class processes.
If this isn't possible, I'll go deeper into the processing algorithm.
Try method chaining.
Ex:
var s = " YoUr StRiNg".Trim().ToLower();
Cyberdrew has the right idea. With string being immutable, you'll be allocating memory during both of those calls regardless. One thing I'd like to suggest, if you're going to call string.Trim().ToLower()
in many locations in your code, is to simplify your calls with extension methods. For example:
public static class MyExtensions
{
public static string TrimAndLower(this String str)
{
return str.Trim().ToLower();
}
}
Here's my attempt. But before I would check this in, I would ask two very important questions.
Are sequential "String.Trim" and "String.ToLower" calls really impacting the performance of my app? Would anyone notice if this algorithm was twice as slow or twice as fast? The only way to know is to measure the performance of my code and compare against pre-set performance goals. Otherwise, micro-optimizations will generate micro-performance gains.
Just because I wrote an implementation that appears faster, doesn't mean that it really is. The compiler and run-time may have optimizations around common operations that I don't know about. I should compare the running time of my code to what already exists.
static public string TrimAndLower(string str) { if (str == null) { return null; } int i = 0; int j = str.Length - 1; StringBuilder sb; while (i < str.Length) { if (Char.IsWhiteSpace(str[i])) // or say "if (str[i] == ' ')" if you only care about spaces { i++; } else { break; } } while (j > i) { if (Char.IsWhiteSpace(str[j])) // or say "if (str[j] == ' ')" if you only care about spaces { j--; } else { break; } } if (i > j) { return ""; } sb = new StringBuilder(j - i + 1); while (i <= j) { // I was originally check for IsUpper before calling ToLower, probably not needed sb.Append(Char.ToLower(str[i])); i++; } return sb.ToString(); }
If the strings use only ASCII characters, you can look at the C# ToLower Optimization. You could also try a lookup table if you know the character set ahead of time
So first of all, trim first and replace second, so you have to iterate over a smaller string with your ToLower()
other than that, i think your best algorithm would look like this:
- Iterate over the string once, and check
- whether there's any upper case characters
- whether there's whitespace in beginning and end (and count how many chars you're talking about)
- if none of the above, return the original string
- if upper case but no whitespace: do ToLower and return
- if whitespace:
- allocate a new string with the right size (original length - number of white chars)
- fill it in while doing the ToLower
You can try this:
public static void Main (string[] args) {
var str = "fr, En, gB";
Console.WriteLine(str.Replace(" ","").ToLower());
}
精彩评论