大写单词 [英] capitalize words

查看:80
本文介绍了大写单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string a ="我有一个苹果" ;;

i希望输出为我有一个苹果;

i能够将第一个字大写但是无法弄清楚怎么用
来大写该字符串中的每一个字


谢谢,

Aaron

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

推荐答案

a = a.ToUpper();
http://msdn.microsoft.com/library/de...uppertopic.asp


HTH,


比尔
www.tibasolutions.com

" Aaron" <ク***** @ yahoo.com>在消息中写道

新闻:O6 ************* @ TK2MSFTNGP11.phx.gbl ...
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有一个苹果;我希望输出是我有一个苹果;

i能够把第一个词大写但是无法弄清楚如何
大写该字符串中的每个单词

谢谢,
Aaron
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



你可以做一个foreach通过字符串 - foreach(char中的a) - 或者做
a for循环并使用字符串'的索引器。无论哪种方式,您都可以检查每个

字符,并将Char.ToUpper()应用于

空格(Char.IsWhiteSpace())之后的任何字符。你不能修改

的单个字符当然不会创建一个新的字符串,这不是有效的;

你可能想要将每个字符附加到一个StringBuilder并在例程结束时返回

StringBuilder的ToString()。或者,将字符串转换为字符数组可能会更快
,调整必要的

字符,然后将其转换回字符串。你的里程数

可能会因典型的字符串长度而有所不同。


另一种方法可能是使用string的split()方法将其转换为

成一个字符串数组的单词,每个单词都是正确的情况,然后将它们放回原来

一起变成一个字符串。这在概念上很简单,但如果你想要调用这个例程,可能会太慢。


最后,可以构造一个正则表达式做一些或全部这个

工作。


--BOB


Aaron <ク***** @ yahoo.com>在消息中写道

新闻:O6 ************* @ TK2MSFTNGP11.phx.gbl ...
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有一个苹果;我希望输出是我有一个苹果;

i能够把第一个词大写但是无法弄清楚如何
大写该字符串中的每个单词

谢谢,
Aaron
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



如果你是使用VB.Net,查看StrConv()函数及其转换

参数。根据文档,VbStrConv.ProperCase将每个单词的大写字母




我不知道C#等价物。


Tom Dacon

Dacon软件咨询


" Aaron" <ク***** @ yahoo.com>在消息中写道

新闻:O6 ************* @ TK2MSFTNGP11.phx.gbl ...
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有一个苹果;我希望输出是我有一个苹果;

i能够把第一个词大写但是无法弄清楚如何
大写该字符串中的每个单词

谢谢,
Aaron
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



这篇关于大写单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆