473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

capitalize words

string a = "i have an apple";
i want the output to be "I Have An Apple";
i was able to capitalize the first word but can't figure out how to
capitalize every word in that string

Thanks,
Aaron
Jul 21 '05 #1
10 2359
a = a.ToUpper();
http://msdn.microsoft.com/library/de...uppertopic.asp

HTH,

Bill
www.tibasolutions.com
"Aaron" <ku*****@yahoo.com> wrote in message
news:O6*************@TK2MSFTNGP11.phx.gbl...
string a = "i have an apple";
i want the output to be "I Have An Apple";
i was able to capitalize the first word but can't figure out how to
capitalize every word in that string

Thanks,
Aaron

Jul 21 '05 #2
You could do a foreach through the string -- foreach (char c in a) -- or do
a for loop and use the string's indexer. Either way you can examine each
character that way and apply Char.ToUpper() to any character following
whitespace (Char.IsWhiteSpace()). You can't modify individual characters of
the string of course without creating a new string, which isn't efficient;
you might want to append each character to a StringBuilder and return the
StringBuilder's ToString() at the end of the routine. Or, it might be
faster to convert the string to a character array, tweak the necessary
characters, and then convert it back to a string at the end. Your mileage
will likely vary depending on typical string lengths.

Yet another approach might be to use the Split() method of string to turn it
into a string array of words, proper case each word, then put them back
together into a string. That is conceptually simple but may be too slow if
you're going to call this routine alot.

Finally, a regular expression could be constructed to do some or all of this
work.

--Bob

"Aaron" <ku*****@yahoo.com> wrote in message
news:O6*************@TK2MSFTNGP11.phx.gbl...
string a = "i have an apple";
i want the output to be "I Have An Apple";
i was able to capitalize the first word but can't figure out how to
capitalize every word in that string

Thanks,
Aaron

Jul 21 '05 #3
If you're using VB.Net, check out the StrConv() function and its Conversion
argument. VbStrConv.ProperCase, according to the docs, capitalizes each word
of a string.

I'm not aware of a C# equivalent.

Tom Dacon
Dacon Software Consulting

"Aaron" <ku*****@yahoo.com> wrote in message
news:O6*************@TK2MSFTNGP11.phx.gbl...
string a = "i have an apple";
i want the output to be "I Have An Apple";
i was able to capitalize the first word but can't figure out how to
capitalize every word in that string

Thanks,
Aaron

Jul 21 '05 #4
"Aaron" <ku*****@yahoo.com> wrote in
news:O6*************@TK2MSFTNGP11.phx.gbl:
string a = "i have an apple";
i want the output to be "I Have An Apple";
i was able to capitalize the first word but can't figure out how
to capitalize every word in that string


Aaron,

As Tom Dacon pointed out, VB.Net has the StrConv function. What
isn't generally known is that all of those functions that appear to
be only callable from VB.Net can be called from any .Net language,
including C#:
// Add a reference to the Microsoft.VisualBasic.dll to your project.

// For .Net 1.0, it is located in
// C:\WINDOWS\Microsoft.NET\Framework\v1.0.3705

// For .Net 1.1, it is located in
// C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322
using System.Globalization;
using Microsoft.VisualBasic;

....

string a = "i have an apple";
Console.WriteLine(Strings.StrConv(a, VbStrConv.ProperCase,
CultureInfo.CurrentCulture.LCID));

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Jul 21 '05 #5
On Sat, 18 Sep 2004 15:51:56 -0700, Aaron wrote:
string a = "i have an apple";
i want the output to be "I Have An Apple";
i was able to capitalize the first word but can't figure out how to
capitalize every word in that string


Since strings are immutable (for a good reason), you can't say something
like:

a[idx] = Char.ToUpper(a[idx]);

Since this would modify the instance, the assignment can't be made.

You could, on the other hand, assign the variable a to a whole new string
instance (reference):

a = "new string";

However, you aren't actually changing the instance that a referenced before
-- you're simple "pointing" a to a new string instance.

The usual way to work around the immutability problem would be to create a
StringBuilder instance, modify it as desired then return a string from it:

StringBuilder sb = new StringBuilder(a);
sb[idx] = Char.ToUpper(sb[idx]);
a = sb.ToString();

Since StringBuilder instances aren't immutable, you *can* modify them, as
shown.

In this case, though, I would probably just create a StringBuilder instance
and fill it up one character at a time:

/// <summary>
/// Return a copy of the specified string with the first character and
/// any characters following spaces, as upper-case.
/// </summary>
static string MakeIntoTitleCase(string val) {
StringBuilder sb = new StringBuilder();

for (int i = 0; i < val.Length; ++i){
if (i == 0 || val[i - 1] == ' ') {
sb.Append(Char.ToUpper(val[i]));
} else {
sb.Append(val[i]);
}
}

return sb.ToString();
}
Jul 21 '05 #6
On Sat, 18 Sep 2004 15:51:56 -0700, Aaron wrote:
string a = "i have an apple";
i want the output to be "I Have An Apple";
i was able to capitalize the first word but can't figure out how to
capitalize every word in that string

Thanks,
Aaron


using System;
using System.Globalization;

class Test
{
public static void Main ()
{
string s =
CultureInfo.CurrentCulture.TextInfo.ToTitleCase ("i have an apple.");
Console.WriteLine (s);
}
}
--
Tom Shelton [MVP]
Jul 21 '05 #7
Is it possible to traverse the whole string, and when encountering a
space, set the next character to uppercase. With this method ,it must
be made sure that only once space exist between two words.

Maybe there is an easier way how to do it, then someone else will
post, and both of us will learn :)

"Aaron" <ku*****@yahoo.com> wrote in message news:<O6*************@TK2MSFTNGP11.phx.gbl>...
string a = "i have an apple";
i want the output to be "I Have An Apple";
i was able to capitalize the first word but can't figure out how to
capitalize every word in that string

Thanks,
Aaron

Jul 21 '05 #8
"W.G. Ryan" <Wi**********@gmail.com> wrote in message
news:e7**************@TK2MSFTNGP15.phx.gbl...
a = a.ToUpper();
http://msdn.microsoft.com/library/de...uppertopic.asp


That will convert THE ENTIRE STRING TO UPPER CASE, Not Capitalise The First
Letter Of Each Word, which is what the OP requires...

Fortunately this is very easy in C#:

string strTitleCase =
System.Globalization.CultureInfo.CurrentCulture.Te xtInfo.ToTitleCase ("the
string to be converted");
Jul 21 '05 #9
Aaron,

When you use VBNet or import in the other languages the
Microsoft.VisualBasic namespace, you can use both these

Console.WriteLine(Globalization.CultureInfo.Curren tCulture.TextInfo.ToTitleC
ase("i have an apple."))
Console.WriteLine(StrConv("i have an apple.", VbStrConv.ProperCase))

(with C# closed by ;)

I hope this helps?

Cor
Jul 21 '05 #10
Yep, I should have read it more carefully. Thanks, Bill
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:e$**************@TK2MSFTNGP11.phx.gbl...
"W.G. Ryan" <Wi**********@gmail.com> wrote in message
news:e7**************@TK2MSFTNGP15.phx.gbl...
a = a.ToUpper();
http://msdn.microsoft.com/library/de...uppertopic.asp
That will convert THE ENTIRE STRING TO UPPER CASE, Not Capitalise The First Letter Of Each Word, which is what the OP requires...

Fortunately this is very easy in C#:

string strTitleCase =
System.Globalization.CultureInfo.CurrentCulture.Te xtInfo.ToTitleCase ("the
string to be converted");

Jul 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Laurence Nuttall | last post by:
In vb 6 when I typed in if then else endif vb 6 would automatically capitalize the if then else and endif words, ..net does not How can i get .net to capitalize keywords?
1
by: Laurence Nuttall | last post by:
In vb 6 when I typed in if then else endif vb 6 would automatically capitalize the if then else and endif words, ..net does not How can i get .net to capitalize keywords?
10
by: Aaron | last post by:
string a = "i have an apple"; i want the output to be "I Have An Apple"; i was able to capitalize the first word but can't figure out how to capitalize every word in that string Thanks,...
1
by: Laurence Nuttall | last post by:
In vb 6 when I typed in if then else endif vb 6 would automatically capitalize the if then else and endif words, ..net does not How can i get .net to capitalize keywords?
7
by: dizzylizzyd514 | last post by:
I need to make this: introductoRy speEch ==> Introductory Speech pRecalCulus 1 and 2 ==> Precalculus 1 And 2 I know how to capitalize the first letter of a word, but not multiple words in a...
12
by: jackson.rayne | last post by:
Hello, I am a javascript newbie and I'm stick at one place. I have a requirement where I will get a sentence in a variable example var v1 ="This is a sentence"
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.