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.
C#
////// Converts input to as close to sentence\ correct case as possible///////// Free text for case correction///////// Free text converted to sentence\ correct case///publicstringSentenceCase(string input)
{
//Break out if there is not work to doif (string.IsNullOrEmpty(input))
{
return"";
}
//Array of characters that signal the next character should be capitalised
List lineTerminators = new List(newchar[] { '.', ':', '\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 stringchar[] output = newchar[input.Length];
//Character previous to current characterchar cPrev = newchar();
//Character following current characterchar cNext;
//Current characterchar c;
//Loop through input charsfor (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 contractionif (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 outputelseif (!Char.IsLetter(c))
{
output[i] = c;
//Line terminators set start boolean to trueif (lineTerminators.Contains(c))
{
start = true;
}
//Numbers count as starting a new sentence, other characters do notelseif (Char.IsNumber(c) && start)
{
start = false;
}
}
//"I" or "i" with no letters either side are assumed to be personal pronoun "I"elseif ((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 contractionif (cNext.Equals('\''))
{
personalProNoun = true;
}
}
//If the last character was a line terminator then current letter is upper caseelseif (start)
{
start = false;
output[i] = Char.ToUpper(c);
}
//Lower case letters are unchangedelseif (Char.IsLower(c))
{
output[i] = c;
start = false;
}
//Only upper case letters remain nowelse
{
start = false;
//If this is the start of a se4ntence, add the letter as it isif (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 letterselseif (Char.IsUpper(cPrev) || Char.IsUpper(cNext))
{
output[i] = Char.ToLower(c);
}
else
{
output[i] = c;
}
}
cPrev = c;
}
returnnewstring(output);
}
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Danatas GerviRank #4,152 · 361 reputationPosted Aug 3, 2009, 8:24 AM EDT
0
Your code is good. I found only one place to correct
Code
char cPrev = (char)0;// = new char();
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". :-)
pootle8 reputationPosted Aug 3, 2009, 10:57 PM EDT
0
Great stuff - thank you.
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?
pootle8 reputationPosted Aug 3, 2009, 5:59 AM EDT
A pointer on retaining formatting in code blocks would be appreciated too :)
Danatas GerviRank #4,152 · 361 reputationPosted Aug 3, 2009, 8:24 AM EDT
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".
:-)
pootle8 reputationPosted Aug 3, 2009, 10:57 PM EDT
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 GerviRank #4,152 · 361 reputationPosted Aug 4, 2009, 4:57 AM EDT
all, that I found here.
Even "gold" members are using only it (+ font size) for code separating.
:-(