Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to modify the text in a textbox such that if I enter 'husain sabir',it should become 'Husain Sabir'.
This is the code i tried :
C#
public static string CapitalizeFirstLetters(string sValue)
        {
            char[] array = sValue.ToCharArray();
            // handle the first letter in the string
            if (array.Length >= 1)
            {
                if (char.IsLower(array[0]))
                {
                    array[0] = char.ToUpper(array[0]);
                }
            }

            // scan through the letters, checking for spaces
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i - 1] == ' ')
                {
                    if (char.IsLower(array[i]))
                    {
                        array[i] = char.ToUpper(array[i]);
                    }
                }
            }

            return new string(array);
        }


But its not working.Can someone please help me out?
Posted

 
Share this answer
 
v2
Comments
Husain Sabir 17-Dec-12 5:25am    
I'm using the same code.But it isn't working.
its this code creating problems for you.

You are checking like this if (array[i - 1] == ' ')

and assigning like this array[i]

C#
for (int i = 1; i < array.Length; i++)
{
  if (array[i] == ' ')
  {
    if (char.IsLower(array[i+1]))
     {
        array[i+1] = char.ToUpper(array[i+1]); // space is identified at Index i so next i+1 has to converted to Upper!!
      }
    }
}
 
Share this answer
 
v3
Comments
Husain Sabir 17-Dec-12 5:26am    
thats because i is initialzed to 1.
Jibesh 17-Dec-12 5:30am    
Ok sorry I get the point you should not use i-1, just check i and set the toArray i+1 because the char followed by the space has to be converted to Upper. Check my updated solution,
Husain Sabir 17-Dec-12 5:36am    
still not working.
this is what i enter : 'husain sabir'
this is what i get : 'husain sabir'
Jibesh 17-Dec-12 5:42am    
are you sure you code look like this
array[i+1] = char.ToUpper(array[i+1]);
Husain Sabir 17-Dec-12 5:53am    
yes.its is like this.
Here is a sample approach
C#
string Sampleinput = "husain sabir";
       string Output = "";
       string[] inputWords = Sampleinput.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

       foreach (string word in inputWords)
       {
           char[] a = word.ToCharArray();
           a[0] = char.ToUpper(a[0]);
           Output += new string(a) + " ";
       }
       Console.WriteLine(Output);
 
Share this answer
 
Comments
Husain Sabir 17-Dec-12 5:51am    
this one works.but it doesn't show.
i mean when i put a breakpoint and see,the outpu comes as "Husain Sabir".but the textbox still shows it as "husain sabir".
i'm calling the function in the leave event of my textbox like this:
private void txtName_Leave(object sender, EventArgs e)
{
Capital(txtManufacturerName.Text);
}

where 'Capital' is the name of the function where your code is written.
__TR__ 17-Dec-12 5:57am    
What is the Capital method doing? Does it assign the output to the textbox or does it just return the string ?
Husain Sabir 17-Dec-12 6:08am    
got the solution.
and your solution also works.
thanks!
__TR__ 17-Dec-12 6:12am    
You are welcome.
Jibesh 17-Dec-12 6:00am    
You need to set the changed text back to the textbox.Text property to display the modified text. by calling the method you pefromed only the conversion to display it update the TextBox.Text property.
The solution was not in the code but in the way of calling the function.
this is the way i did it:

C#
private void txtName_Leave(object sender, EventArgs e)
        {
            txtManufacturerName.Text = CapitalizeFirstLetters(txtManufacturerName.Text);
        }


thanks everyone for the help.
 
Share this answer
 
check this solution

C#
using System.Globalization;
        private void txtX_Leave(object sender, EventArgs e)
        {
            txtX.Text = ToSentenceCase(txtX.Text);
        }

        public  string ToSentenceCase(string strSen)
        {
            TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
            return myTI.ToTitleCase(strSen);
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900