473,765 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2440
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******** *****@TK2MSFTNG P11.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.IsWhiteSp ace()). 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******** *****@TK2MSFTNG P11.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.Prope rCase, 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******** *****@TK2MSFTNG P11.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******** *****@TK2MSFTNG P11.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.Visua lBasic.dll to your project.

// For .Net 1.0, it is located in
// C:\WINDOWS\Micr osoft.NET\Frame work\v1.0.3705

// For .Net 1.1, it is located in
// C:\WINDOWS\Micr osoft.NET\Frame work\v1.1.4322
using System.Globaliz ation;
using Microsoft.Visua lBasic;

....

string a = "i have an apple";
Console.WriteLi ne(Strings.StrC onv(a, VbStrConv.Prope rCase,
CultureInfo.Cur rentCulture.LCI D));

--
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 MakeIntoTitleCa se(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.Globaliz ation;

class Test
{
public static void Main ()
{
string s =
CultureInfo.Cur rentCulture.Tex tInfo.ToTitleCa se ("i have an apple.");
Console.WriteLi ne (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******* ******@TK2MSFTN GP11.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**********@g mail.com> wrote in message
news:e7******** ******@TK2MSFTN GP15.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.Globaliz ation.CultureIn fo.CurrentCultu re.TextInfo.ToT itleCase ("the
string to be converted");
Jul 21 '05 #9
Aaron,

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

Console.WriteLi ne(Globalizatio n.CultureInfo.C urrentCulture.T extInfo.ToTitle C
ase("i have an apple."))
Console.WriteLi ne(StrConv("i have an apple.", VbStrConv.Prope rCase))

(with C# closed by ;)

I hope this helps?

Cor
Jul 21 '05 #10

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

Similar topics

1
262
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
1389
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
9859
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, Aaron
1
1595
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
16129
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 string like the examples shown. Any ideas????
12
4001
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
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10161
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.