VS 2008
Hi
I have written a string manipulation function in C#. I am very new to C# and pretty out of practice with .NET in general. Please could you review the below. I am interested in (in order of importance):
1) Fundamental programming errors\ bad practice
2) Optimisations
3) Logical errors
This will eventually make it into a CLR function and so will need to be as optimised as possible.
I hope someone is able to give me some pointers on this - there appear to be so many different ways of manipulating strings.
/// /// Converts input to as close to sentence\ correct case as possible /// /// /// Free text for case correction /// /// /// Free text converted to sentence\ correct case /// public string SentenceCase(string input) {
//Break out if there is not work to do if (string.IsNullOrEmpty(input)) { return ""; }
//Array of characters that signal the next character should be capitalised List lineTerminators = new List(new char[] { '.', ':', '\n', '\r', '!', '?' }); //Indicates if previous character was a lineTerminators Boolean start = true; //Indicates that currently parsing personal pronoun contraction (e.g. I'll, 'I'd, I'm etc.) Boolean personalProNoun = false; //outputed string char[] output = new char[input.Length]; //Character previous to current character char cPrev = new char(); //Character following current character char cNext; //Current character char c;
//Loop through input chars for (int i = 0; i < input.Length; i++) {
c = input[i]; cNext = i < (input.Length - 1) ? input[i + 1] : '.';
//current character is always lower case if part of personal pronoun contraction if (personalProNoun) { output[i] = Char.ToLower(c); start = false; //Set personalProNoun to false if the next character is not a letter personalProNoun = Char.IsLetter(cNext); } //any non-letter characters are added to output else if (!Char.IsLetter(c)) { output[i] = c;
//Line terminators set start boolean to true if (lineTerminators.Contains(c)) { start = true; } //Numbers count as starting a new sentence, other characters do not else if (Char.IsNumber(c) && start) { start = false; } } //"I" or "i" with no letters either side are assumed to be personal pronoun "I" else if ((c.Equals('I') || c.Equals('i')) && !Char.IsLetter(cPrev) && !Char.IsLetter(cNext)) { //Append "I" in upper case output[i] = Char.ToUpper(c); start = false;
//If the following character is apostrophe we are starting a personal pronoun contraction if (cNext.Equals('\'')) { personalProNoun = true; } } //If the last character was a line terminator then current letter is upper case else if (start) { start = false; output[i] = Char.ToUpper(c); } //Lower case letters are unchanged else if (Char.IsLower(c)) { output[i] = c; start = false; } //Only upper case letters remain now else { start = false;
//If this is the start of a se4ntence, add the letter as it is if (start) { output[i] = c; } //if there are uppercase letters either side of this letter, convert to lower case //This allows us to retain the odd upper case character when the freee text is not //solely made up fo upper case letters else if (Char.IsUpper(cPrev) || Char.IsUpper(cNext)) { output[i] = Char.ToLower(c); } else { output[i] = c; } }
cPrev = c;
}
return new string(output);
}
|
Danatas GerviPosted Aug 4, 2009, 8:57 AM
all, that I found here.
Even "gold" members are using only it (+ font size) for code separating.
:-(
pootlePosted Aug 4, 2009, 2:57 AM
Even a "yeah - that's ok" is really useful. My skill set is SQL Server and I see so many f*** ups when people try to program in T-SQL I wanted to make sure I've not done the same the other way.
Anyhoo - I'll make that change. I spotted a logical error I need to sort out too.
For future reference, do you know how to retain formatting when pasting into this forum?
Danatas GerviPosted Aug 3, 2009, 12:24 PM
I found only one place to correct
variable should be inited, correct, but using new operator forses to create it in heap, and it takes a little more time.
If you will init local variables (simple type) directly, it will be allocated in stack.
But, this is "nano-optimisation".
:-)
pootlePosted Aug 3, 2009, 9:59 AM
A pointer on retaining formatting in code blocks would be appreciated too :)