将句子改为大写案例。 [英] Changing case in a sentence to Capitalize Case.

查看:59
本文介绍了将句子改为大写案例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我是一个javascript新手,我会坚持到一个地方。


我有一个要求我在哪里将在变量中得到一个句子


示例


var v1 ="这是一个句子


现在我必须将句子更改为大写的情况,其中每个单词的第一个

字母将在大写字母中


所以对于上面的例子输出应该是


这是一个句子


我找到了一些可以将大小写改为大写的脚本或

小写,但我无法为此提出解决方案。


还有一件事,我将获得的字数没有限制<这句话中的
。我可能会得到一个字甚至十个字。


我正在寻找适用于所有场景的解决方案,


问候,

Rayne

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"

Now I have to change the sentence to Capitalize case where the first
alphabet of every word will be in caps

So for above example the output should be

This Is A Sentence

I have found some scripts that can change case to uppercase or
lowercase but I''m not able to come up with a solution for this.

One more thing, there is no limit on the number of words that I''ll get
in the sentence. I may get one word or even ten words.

I''m looking for a solution that will work for all scenarios,

Regards,
Rayne

推荐答案

2008-09-22 18:19, ja *********** @ gmail.com 写道:
On 2008-09-22 18:19, ja***********@gmail.com wrote:

var v1 ="这是一个句子


现在我必须将句子更改为大写的情况,其中每个字母的第一个

字母表单词将在大写字母
var v1 ="This is a sentence"

Now I have to change the sentence to Capitalize case where the first
alphabet of every word will be in caps



v1 = v1.replace(/ \b(\ w)/ g,function(s,c){

返回c.toUpperCase();

});


BTW,你的问题看起来像是典型的家庭作业。如果那个'

的情况:让其他人解决你的初学者作业不是一个聪明的想法,如果你想学习这门语言或者必须通过
以后
考试。如果这不是作业,请忽略。

- 康拉德

v1 = v1.replace(/\b(\w)/g, function (s, c) {
return c.toUpperCase();
});

BTW, your question looks like a typical homework assignment. If that''s
the case: letting other people solve your beginner assignments is not a
not a clever idea, if you want to learn the language or have to pass
exams later. If this wasn''t homework, please disregard.
- Conrad


ja *********** @ gmail.com 写道:

I有一个要求,我将在一个变量中得到一个句子


例子


var v1 ="这是一个句子


现在我必须将句子更改为大写的情况,其中每个单词的第一个

字母将为大写字母
I have a requirement where I will get a sentence in a variable

example

var v1 ="This is a sentence"

Now I have to change the sentence to Capitalize case where the first
alphabet of every word will be in caps



你的意思是_letter_; *不是*字母,这是一组字母。


< http://en.wikipedia.org/wiki/Alphabet>

You mean _letter_; *not* alphabet, which is a set of letters.

<http://en.wikipedia.org/wiki/Alphabet>


所以对于上面的例子,输出应该是


这是一个句子
So for above example the output should be

This Is A Sentence



v1 = v1。替换(/(^ | \)([az])/ g,

函数(m,p1,p2){

返回p1 + p2.toUpperCase( );

});


您可以根据自己的需要调整角色等级。

PointedEars

-

var bugRiddenCrashPronePieceOfJunk =(

navigator.userAgent.indexOf(''MSIE 5'')!= -1

&& navigator.userAgent.indexOf(''Mac'')!= -1

)// Plone,register_function.js:16

v1 = v1.replace(/(^|\s)([a-z])/g,
function(m, p1, p2) {
return p1 + p2.toUpperCase();
});

You may adapt the character class to fit your needs.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf(''MSIE 5'') != -1
&& navigator.userAgent.indexOf(''Mac'') != -1
) // Plone, register_function.js:16


< ja *********** @ gmail.comschreef in bericht

news:b2 **************** ****************** @ z6g2000p re.googlegroups.com ...
<ja***********@gmail.comschreef in bericht
news:b2**********************************@z6g2000p re.googlegroups.com...

您好,


我是一个javascript新手我会坚持到一个地方。


我有一个要求,我会在变量中得到一个句子


例子


var v1 ="这是一个句子


现在我必须将句子更改为大写的情况下第一个

每个单词的字母将是大写的


所以对于上面的例子,输出应该是


这是一个句子



a)正则表达式,但我不知道如何使用它们。


b)1:将字符串拆分为单词; 2:首字母大写; 3:连接

单词到字符串。

1:查找var wordarray = []; wordarray = v1.split('''')将执行

2:对于所有k:wordarray [k] = wordarray [k] .charAt(0).toUpperCase()+

wordarray [k] .substr(1);

3:查看连接函数:output = wordarray.join('''');


Tom

a) Regular Expressions, but I don''t know how to use them.

b) 1:Split string into words; 2:capitalize first letters; 3:concatenate
words into string.
1: look up what var wordarray = []; wordarray = v1.split('' '') will do
2: for all k: wordarray[k] = wordarray[k].charAt(0).toUpperCase() +
wordarray[k].substr(1);
3: check out the join function: output = wordarray.join('' '');

Tom


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

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