Click to See Complete Forum and Search --> : [C#] Improving my ToProper() Function
wey97
Sep 27th, 2006, 08:07 AM
Can you code it better?
public string ToProper(string src)
{
string[] s = src.Split(new char[] { ' ' });
string retval = String.Empty;
int n = 0;
foreach (string str in s)
{
if (str == String.Empty)
{
retval += " ";
}
else
{
retval += (str[0].ToString().ToUpper() +
str.Substring(1, str.Length - 1).ToLower() +
(n < s.Length - 1 ? " " : ""));
}
n++;
}
return retval;
}
wild_bill
Sep 27th, 2006, 05:55 PM
This is a little shorter
using System.Text.RegularExpressions;
public string ToPropper(string src)
{
src = " " + src;
foreach (Match mt in Regex.Matches(src," [a-z]"))
{
src = src.Replace(mt.Value,mt.Value.ToUpper());
}
return src.TrimStart();
}
wey97
Sep 28th, 2006, 07:30 AM
This is a little shorter
using System.Text.RegularExpressions;
public string ToPropper(string src)
{
src = " " + src;
foreach (Match mt in Regex.Matches(src," [a-z]"))
{
src = src.Replace(mt.Value,mt.Value.ToUpper());
}
return src.TrimStart();
}
I like the idea of using regular expressions, but I think you misunderstood what I was trying to do with the function. A ToProper() Function should analyze all letters in a string, capitalize the first letter of each word, and convert additional trailing letters to lower case. It should also ignore all spaces and non alpha characters.
For example, with this input:
ToProper(@"THiS IS aN iMprOPEr Case STRING")
the function should return:
"This Is An Improper Case String"
wild_bill
Sep 28th, 2006, 09:32 AM
Easy enough fix:
public string ToPropper(string src)
{
src = " " + src.ToLower();
foreach (Match mt in Regex.Matches(src," [a-z]"))
{
//replace first letter of each word with uppercase
src = src.Replace(mt.Value,mt.Value.ToUpper());
}
return src.TrimStart();
}
wey97
Sep 28th, 2006, 12:31 PM
Easy enough fix:
public string ToPropper(string src)
{
src = " " + src.ToLower();
foreach (Match mt in Regex.Matches(src," [a-z]"))
{
//replace first letter of each word with uppercase
src = src.Replace(mt.Value,mt.Value.ToUpper());
}
return src.TrimStart();
}
Your code works if you only have spaces before a letter but what about tabs, and new lines?
Also it would seem to be inefficient. This code eliminates the scan to convert to all lower case.
The regular expression "\w" matches words:
public string ToProper(string src)
{
// match one or more words
foreach (Match mt in Regex.Matches(src, @"\w+"))
{
src = src.Replace(mt.Value, mt.Value.Substring(0, 1).ToUpper() +
mt.Value.Substring(1, mt.Value.Length - 1).ToLower());
}
return src;
}
Pass the function this string and you'll see a problem:
ToProper("THIS \nwOrD haS \ta hyPhen: half-baked")
The newline, tab, and spaces are all OK but when the 'a' by itself is found, it replaces all other lowercase 'a's with 'A' so the result is:
"This \nWord HAs \tA Hyphen: HAlf-BAked"
and another problem is evident: 'half' and 'baked' was recognized as two separate words even though it's one hyphenated word.
This leads to the following:
public string ToProper(string src)
{
string skip = " \t\n";
// match zero or one whitespace followed by one or more words
// followed by zero or one hyphen followed by zero or one tic followed
// by zero or more words followed by zero or more whitespaces
foreach (Match mt in Regex.Matches(src, @"\s?\w+-?'?\w*\s*"))
{
//if the first character is a white space character
if (skip.IndexOf(mt.Value[0]) >= 0)
{
src = src.Replace(mt.Value, mt.Value.Substring(0, 2).ToUpper() +
mt.Value.Substring(2, mt.Value.Length - 2).ToLower());
}
else
{
src = src.Replace(mt.Value, mt.Value.Substring(0, 1).ToUpper() +
mt.Value.Substring(1, mt.Value.Length - 1).ToLower());
}
}
return src;
}
{EDIT}
Didn't realize there was a Char.IsWhiteSpace(char) Function:
public string ToProper(String src)
{
// match zero or one whitespace followed by one or more words
// followed by zero or one - followed by zero or one ' followed
// by zero or more words followed by zero or more whitespaces
foreach (Match mt in Regex.Matches(src, @"\s?\w+-?'?\w*\s*"))
{
//if the first character is a white space character
if(Char.IsWhiteSpace(mt.Value[0]))
{
src = src.Replace(mt.Value, mt.Value.Substring(0, 2).ToUpper() +
mt.Value.Substring(2, mt.Value.Length - 2).ToLower());
}
else
{
src = src.Replace(mt.Value, mt.Value.Substring(0, 1).ToUpper() +
mt.Value.Substring(1, mt.Value.Length - 1).ToLower());
}
}
return src;
}
Now see if you can code this better.
wild_bill
Sep 28th, 2006, 02:33 PM
How about this?
public string ToPropper(string src)
{
//need to append character at the end of the string, as the
//regex pattern will remove the last word
src = src + " a";
//create buffer to store new text
System.Text.StringBuilder sb = new System.Text.StringBuilder();
//Groups
foreach (Match mt in Regex.Matches(src,"(\\b\\w)(.*?)(?=\\b\\w)",RegexOptions.Singleline))
{
//replace first letter of each match with uppercase
sb.Append(mt.Groups[1].Value.ToUpper());
//replace everything else with lowercase
sb.Append(mt.Groups[2].Value.ToLower());
}
return sb.ToString();
}
wey97
Sep 28th, 2006, 02:50 PM
How about this?
That doesn't work for the string I posted above.
wild_bill
Sep 28th, 2006, 04:15 PM
Try it again, I changed the search expression.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.