473,410 Members | 1,875 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,410 software developers and data experts.

I have a string, and I want let the first character capitalize. How to do it?

Sorry for this stupid question.
I am using c way s[0] = s[0] -'a' +'A'

but I can't compile it.

Thanks for your kind help
Best wishes

Mar 14 '07 #1
5 1222
Strings are immutable; you cannot update them. And character
arithmetic isn't very culture safe.

You either need to take the two substrings and use ToUpper() on the
first portion (character), then concatenate, or (depdning on usage)
you might look at TextInfo.ToTitleCase().

Marc
Mar 14 '07 #2

"fAnSKyer/C# newbie" <fa******@gmail.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
Sorry for this stupid question.
I am using c way s[0] = s[0] -'a' +'A'

but I can't compile it.
Use a StringBuilder instead.
>
Thanks for your kind help
Best wishes

Mar 14 '07 #3
http://msdn2.microsoft.com/en-us/lib....textinfo.aspx

"fAnSKyer/C# newbie" <fa******@gmail.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
Sorry for this stupid question.
I am using c way s[0] = s[0] -'a' +'A'

but I can't compile it.

Thanks for your kind help
Best wishes

Mar 14 '07 #4
Ben Voigt <rb*@nospam.nospamwrote:
>
"fAnSKyer/C# newbie" <fa******@gmail.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
Sorry for this stupid question.
I am using c way s[0] = s[0] -'a' +'A'

but I can't compile it.

Use a StringBuilder instead.
There's no point in using a StringBuilder here:

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

Okay, there's a box involved, but there won't be any wasted space
(easily avoided with StringBuilder, admittedly), and you won't be
creating a StringBuilder object.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 14 '07 #5
Very cool method, works fine. Thanks a lot and all the other fellows:P
On Mar 14, 1:14 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
Ben Voigt <r...@nospam.nospamwrote:
"fAnSKyer/C# newbie" <fansk...@gmail.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
Sorry for this stupid question.
I am using c way s[0] = s[0] -'a' +'A'
but I can't compile it.
Use a StringBuilder instead.

There's no point in using a StringBuilder here:

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

Okay, there's a box involved, but there won't be any wasted space
(easily avoided with StringBuilder, admittedly), and you won't be
creating a StringBuilder object.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 14 '07 #6

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

Similar topics

5
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;...
8
by: Mr. B | last post by:
In VB6, I had some code which 'forced' the first character of a string entered to be Capital. For example, if a person was entering their name (john doe)... the code would 'force' --- John Doe. ...
7
by: Kerri | last post by:
Hi, I have a string 'hello world' I can UCase my string using String.ToUpper Is there anyway to just UCase the first letter ofe ach word? Thanks,
3
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
by: Aaron | last post by:
1. How do I remove non-alphanumeric characters? Input: dEFD&*fdff/df; output: dEFDfdffdf 2. How can I capitalize the first character of a word? Input: remember Output: Remember Thanks
5
by: DDK | last post by:
What is the best way to capitalize the first letter of a string? Thanks for any help, d.
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
1
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
by: Robert Johnson | last post by:
Hi all. Checked the docs and all I can find is UCase but that will convert everything to upper. What I want is when my users enter say a name into a text box I want to just upper the first char for...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.