Results 1 to 8 of 8

Thread: [C#] Improving my ToProper() Function

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    [C#] Improving my ToProper() Function

    Can you code it better?

    Code:
    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;
    }
    Last edited by wey97; Oct 12th, 2006 at 02:21 PM.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [C#] Improving my ToProper() Function

    This is a little shorter
    Code:
    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();
    		}

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [C#] Improving my ToProper() Function

    Quote Originally Posted by wild_bill
    This is a little shorter
    Code:
    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:
    Code:
    ToProper(@"THiS IS aN iMprOPEr Case STRING")
    the function should return:
    Code:
    "This Is An Improper Case String"

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [C#] Improving my ToProper() Function

    Easy enough fix:
    Code:
    		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();
    		}

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [C#] Improving my ToProper() Function

    Quote Originally Posted by wild_bill
    Easy enough fix:
    Code:
    		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:
    Code:
    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:
    Code:
    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:
    Code:
    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.
    Last edited by wey97; Sep 28th, 2006 at 01:32 PM.

  6. #6
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [C#] Improving my ToProper() Function

    How about this?
    Code:
    		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();
    		}
    Last edited by wild_bill; Sep 28th, 2006 at 04:15 PM.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276

    Re: [C#] Improving my ToProper() Function

    Quote Originally Posted by wild_bill
    How about this?
    That doesn't work for the string I posted above.
    Last edited by wey97; Sep 28th, 2006 at 04:21 PM.

  8. #8
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [C#] Improving my ToProper() Function

    Try it again, I changed the search expression.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width