hi...im trying to captilize the first word of a line that was built with stringbuilder, but cant find the right way...
do i need to copy the word into a regular string and use ToUpper()?
it drive me nuts...
thanks for your help...Nuninio

You can split line by ' ' space, get words[0] -first word- capitalize it, then return this array to 1 string again.

Try this in a console app:
StringBuilder sb = new StringBuilder("this is a test");
string str = sb.ToString();
char ch = str[0];
sb.Replace(ch, char.ToUpper(ch), 0, 1);

commented: Always helpful, correct +6

Danny :) he said

im trying to captilize the first word of a line

not the first character!

I know Ramy. I was just posting a short snippet. So he could carry on.
Get the first word, put its chars in a loop and upper them. This would be my approach.
You may always correct me, if I'm wrong. I am just a learner among learners.

StringBuilder is (as you know) an array of char.
So you can either loop through getting the first space to detect the word, and start looking for the return\line feed '\r\n' or just convert it to string.

Here are a couple ways.
If only one line is in the stringbuilder (dumb & ugly)

sb = new StringBuilder("alpha beta");
            string result = sb.ToString().Split(' ')[0].ToUpper()
                + sb.ToString().Substring(sb.ToString().Split(' ')[0].Length);

If more than one line in the stringbuilder:

StringBuilder sb = new StringBuilder();
            string text = string.Empty;
            char[] CRLF = new char[2] { '\r', '\n' };
            sb.AppendLine("Alpha One");
            sb.AppendLine("Bravo Two");
            sb.AppendLine("Charlie Three");
            string[] lines = sb.ToString().Split(CRLF,StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < lines.Length; i++)
                string[] words = lines[i].Split(' ');
                text += words[0].ToUpper() + lines[i].Substring(words[0].Length) + Environment.NewLine;
// text contains your text with the first word capitalize for each line.

PS: Hi Ramy, been a long time....

commented: completely right +6

@Danny: Don't say that man, I am your student I'm not blushing to say that, I consider you from experts I learn from, thanks man :)
But here I just misunderstand you :$

@Jerry: Hey, my friend, I too miss you really, keep in touch :)

StringBuilder is (as you know) an array of char.

StringBuilder is a class which, perhaps under the hood manipulates an array of chars, but I don't know that, nobody ever will or has to know. The StringBuilder ToString() method is what makes it into a more manageable "array of chars" (but it still is not!) Use the String class ToCharArray method for that.
Fine code samples though.:cool:

Hi Ramy really appreciate your kindness, but I hope you don't start thinking I invented computer science, I am just the next guy around the corner!:)


Just for clarifiation, StringBuilder is exactly that, a mutable array of chars. Mutable meaning that is has internal facilities to expand its size in contrast to a fixed array. These internal facilities are really not much different than the manual method of adding a new element to a fixed array.

The class creates a buffer of (n) length (default is 16, max is Int.MaxValue). Everytime the Append... methods are used, it will try to add the chars to the buffer. If the buffer is not large enough, it will allocate more memory, and reload the chars into the new allocation. This is the biggest problem with using this class. Programmers, tend to use the default and this results in memory thrashing and a decrease in performance. Certainly not recommended for Mobile / Smartphone or embedded programming.

So yes, programmers need to know what is under the hood. To use this class efficiently, they should allocate enough space up front to reduce the reallocation process as much as possible.

Thanks for the complement on the code snips, I would do it differently in a production system by reducing the number of Split operations. Probably would just iterate through looking for the first space after a CRLF, and alter the chars for the first word as you describe in your snippet.


OK Jerry,
You are absolutely right here.
I got a bit confused about the immutability of strings and it was already to late in the evening...
Kind regards,

If you're that worried about StringBuilder reallocating its buffer, just use something to build your strings that doesn't require any reallocation at all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.