473,486 Members | 1,850 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Capitalize the firs letter of a string...

DDK
What is the best way to capitalize the first letter of a string?

Thanks for any help,
d.
Nov 15 '05 #1
5 8593
DDK wrote:

What is the best way to capitalize the first letter of a string?

Thanks for any help,
d.


s = s.Length>0 ? (s[0].ToUpper() + (s.Length>1 ? s.Substring(1, s.Length-1) :
"") : s;
Nov 15 '05 #2
DDK wrote:
What is the best way to capitalize the first letter of a string?

Thanks for any help,
d.


Here's *a* way:

static void Main(string[] args)
{
Console.WriteLine(MakeFirstCharUpperCase("test"));
Console.ReadLine();
}

private static string MakeFirstCharUpperCase(string s)
{
StringBuilder sb = new StringBuilder(s);
sb[0] = Char.ToUpper(sb[0]);
return sb.ToString();
}
Nov 15 '05 #3
DDK
Thanks for the examples.

I just modified an example I found, I use a label control, as follows:
string strInput = "lowercase";

// if the first char is lower case

if (char.IsLower(strInput[0]))

// capitalize it

strInput = char.ToUpper(strInput[0]) + strInput.Substring(1,
strInput.Length-1);

lblFirstLetterToUpper.Text = strInput.ToString();

"DDK" <dd*******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
What is the best way to capitalize the first letter of a string?

Thanks for any help,
d.

Nov 15 '05 #4
DDK wrote:
Thanks for the examples.

I just modified an example I found, I use a label control, as follows:
string strInput = "lowercase";

// if the first char is lower case

if (char.IsLower(strInput[0]))

// capitalize it

strInput = char.ToUpper(strInput[0]) + strInput.Substring(1,
strInput.Length-1);
Here you can use the following instead:

strInput = char.ToUpper(strInput[0]) + strInput.Substring(1);

This demonstrates another overload of String.Substring() which only
takes one parameter - a start index. In this case, it returns the
sub-string from index 1 to the end of the string.
lblFirstLetterToUpper.Text = strInput.ToString();

Nov 15 '05 #5
DDK <dd*******@hotmail.com> wrote:
What is the best way to capitalize the first letter of a string?


As well as the previous responses, you may wish to consider the
following if you're doing this a lot - it avoids creating an extra
string unnecessarily:

static string UpperCaseFirstLetter (string original)
{
if (original.Length==0)
{
return original;
}
char originalFirst = original[0];
char upperFirst = char.ToUpper(originalFirst);

if (originalFirst==upperFirst)
{
return original;
}

return upperFirst + original.Substring(1);
}

Note that that will perform the upper casing in the current culture -
you could write another version which specified which culture to use
(and used it in the call to char.ToUpper).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

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

Similar topics

5
20050
by: Rick | last post by:
For some reason my function to capitalize the first letter in a string and keep all the other letters lowercase isn't working. This is what I have. string convname(string name) { int len;...
3
23188
by: news.individual.net | last post by:
Hi! Can I capitalize the first letter of the first paragraph without using a special class for that <p> ? I tried it with this: body > p:first-child:first-letter { font-size: 270%;
3
5961
by: gil | last post by:
I have a script that will capitalize the first letter of every word in my descriptions. But some of my descriptions contain ( ) and /. For example bezel (black). I need the script to capitalize...
1
4084
by: Dwight Shubert | last post by:
Hi all, This is my first try using Access. I understand how to format a text field for uppercase and lowercase, but how do you capitalize only the first letter of each word? tia Dwight
1
1575
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
6258
blyxx86
by: blyxx86 | last post by:
I have the code to capitalize an entire string when it is input, but would like to capitalize just the first letter of a box, or match the case of a drop down list.. My current code is thus: ...
7
16121
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
3956
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"
12
12313
by: dissectcode | last post by:
I know about toupper() and tolower() but I need to capitalize (or de-capitalize)the first letter of a word in a header file. I know there are no preprocessor functions that do that but is there...
0
7094
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,...
0
6964
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...
0
5427
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,...
1
4863
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...
0
4559
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...
0
3066
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...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.