更新字符串/数组项 [英] Updating string/array items
问题描述
嗨 -
我不完全熟悉
JavaScript的规范和标准库,所以如果答案是昨天'的消息,请忽略。
我正在尝试编写一个简单的文本格式化功能来制作标题
正确的案例 - 即第一封信单词大写。
我第一次尝试这个...
函数inProperCase
{
var result,i,k;
i = 0; result = s.toLowerCase();
while((k = result.indexOf('''',i)) - 1){
if (k - i 0){
result [i] = result [i] .toUpperCase();
}
i = k + 1 ;
}
if(result.length - i 1){
result [i] = result [i]。 toUpperCase();
}
返回结果;
}
。 ..但它没有用。令我惊讶的是,你不能通过访问具有索引的字符来设置
字符串中的字符,如下所示:
s [i] = ch;
必须是因为JavaScript字符串是不可变的或EtchedInStone或EtchedInStone。或者
。 (开个玩笑,没有火焰,请。我确定它更有效率。)
我唯一想到的就是这个(有一些细节,比如
复合而谓词,被朋友称为C-bigot后被修改为更易读的形式: ))看起来相当笨重的所有
字符串切片和连接:
函数inProperCase
{
var result = s.toLowerCase(),
i = 0,k = result.indexOf('''',i);
while(k -1){
if(k - i 0){
result = result.substring(0,i)+
result [i] .toUpperCase()+
result.substring(i + 1,result.length);
}
i = k + 1;
k = result.indexOf('''',i);
}
if( result.length - i 1){
result = result.substring(0,i)+
result [i] .toUpperCase()+
result.su bstring(i + 1,result.length);
}
返回结果;
}
您将此称为...
inProperCase("女士我是亚当;
......你得到......
"女士我是亚当
有什么建议让这个更优雅吗?
BTW你可以指点我文库"具有超高效的实现
这个函数的
,但这不是重点。我的问题是,我们如何在JavaScript中更新字符串中的单个字符(或数组中的单个项目)?
它是否类似......
s.wouldYouCareToUpdateThisChar(i,ch); //:D(开玩笑,平静
下来......)
-or-
s .setCharAt(i,ch);
?
谢谢!
- Ken
-
使用 http://www.talkaboutprogramming.com/...ng.javascript/
更多信息,请访问 http://www.talkaboutprogramming.com/faq.html
Hi --
I''m not entirely familiar with the norms and standard libraries of
JavaScript so if the answer to this is yesterday''s news, please ignore.
I''m trying to write a simple text formatting function to make headings
proper case -- i.e. first letter of words capitalized.
I first tried this...
function inProperCase(s)
{
var result, i, k;
i = 0; result = s.toLowerCase();
while ((k = result.indexOf('' '', i)) -1) {
if (k - i 0) {
result[i] = result[i].toUpperCase();
}
i = k + 1;
}
if (result.length - i 1) {
result[i] = result[i].toUpperCase();
}
return result;
}
...but it didn''t work. To my amazement, you cannot set a character in a
string by accessing that character with an index, like so:
s[i] = ch;
Must be because JavaScript strings are "Immutable" or "EtchedInStone" or
something. (Just kidding, no flames, please. I''m sure it is more
efficient.)
The only thing I could come up with was this (with some details, like the
"compound" while predicate, modified into a more readable form after being
called a "C-bigot" by a friend :)) which seems pretty heavy-handed with all
the string slicing and concatting:
function inProperCase(s)
{
var result = s.toLowerCase(),
i = 0, k = result.indexOf('' '', i);
while (k -1) {
if (k - i 0) {
result = result.substring(0, i) +
result[i].toUpperCase() +
result.substring(i+1, result.length);
}
i = k + 1;
k = result.indexOf('' '', i);
}
if (result.length - i 1) {
result = result.substring(0, i) +
result[i].toUpperCase() +
result.substring(i+1, result.length);
}
return result;
}
You call this as...
inProperCase(" madam i''m adam ");
...and you get...
" Madam I''m Adam "
Any suggestions for making this a bit more elegant?
BTW You may point me to a "library" with a super-efficient implementation
of this function, but that is not the point. My issue is, how do we update
single characters in a string (or single items in an array) in JavaScript?
Is it something like...
s.wouldYouCareToUpdateThisChar(i, ch); // :D (just kidding, calm
down...)
-or-
s.setCharAt(i, ch);
?
Thanks!
-- Ken
--
Message posted using http://www.talkaboutprogramming.com/...ng.javascript/
More information at http://www.talkaboutprogramming.com/faq.html
推荐答案
Xcriber51 apensétrès堡:
Xcriber51 a pensé très fort :
嗨 -
我不是很熟悉使用
JavaScript的规范和标准库如果对此的回答是昨天的新闻,请忽略。
我正在尝试写一个简单的文本格式化功能来制作标题
正确的案例 - 即fi第一个字母大写。
我第一次尝试这个...
函数inProperCase
{
var result,i,k;
i = 0; result = s.toLowerCase();
while((k = result.indexOf('''',i)) - 1){
if (k - i 0){
result [i] = result [i] .toUpperCase();
}
i = k + 1 ;
}
if(result.length - i 1){
result [i] = result [i]。 toUpperCase();
}
返回结果;
}
。但它没有用。令我惊讶的是,你不能通过访问具有索引的字符来设置
字符串中的字符,如下所示:
s [i] = ch;
必须是因为JavaScript字符串是不可变的或EtchedInStone或EtchedInStone。或者
。 (开个玩笑,没有火焰,请。我确定它更有效率。)
我唯一想到的就是这个(有一些细节,比如
复合而谓词,被朋友称为C-bigot后被修改为更易读的形式: ))看起来相当笨重的所有
字符串切片和连接:
函数inProperCase
{
var result = s.toLowerCase(),
i = 0,k = result.indexOf('''',i);
while(k -1){
if(k - i 0){
result = result.substring(0,i)+
result [i] .toUpperCase()+
result.substring(i + 1,result.length);
}
i = k + 1;
k = result.indexOf('''',i);
}
if( result.length - i 1){
result = result.substring(0,i)+
result [i] .toUpperCase()+
result.substring(i + 1,result。长度);
}
返回结果;
}
你打电话这是...
inProperCase("女士我是亚当;
..你得到......
"女士我是亚当
有什么建议让这个更优雅吗?
BTW你可以指点我文库"具有超高效的实现
这个函数的
,但这不是重点。我的问题是,我们如何在JavaScript中更新字符串中的单个字符(或数组中的单个项目)?
它是否类似......
s.wouldYouCareToUpdateThisChar(i,ch); //:D(开玩笑,平静
下来......)
-or-
s .setCharAt(i,ch);
?
谢谢!
- Ken
Hi --
I''m not entirely familiar with the norms and standard libraries of
JavaScript so if the answer to this is yesterday''s news, please ignore.
I''m trying to write a simple text formatting function to make headings
proper case -- i.e. first letter of words capitalized.
I first tried this...
function inProperCase(s)
{
var result, i, k;
i = 0; result = s.toLowerCase();
while ((k = result.indexOf('' '', i)) -1) {
if (k - i 0) {
result[i] = result[i].toUpperCase();
}
i = k + 1;
}
if (result.length - i 1) {
result[i] = result[i].toUpperCase();
}
return result;
}
..but it didn''t work. To my amazement, you cannot set a character in a
string by accessing that character with an index, like so:
s[i] = ch;
Must be because JavaScript strings are "Immutable" or "EtchedInStone" or
something. (Just kidding, no flames, please. I''m sure it is more
efficient.)
The only thing I could come up with was this (with some details, like the
"compound" while predicate, modified into a more readable form after being
called a "C-bigot" by a friend :)) which seems pretty heavy-handed with all
the string slicing and concatting:
function inProperCase(s)
{
var result = s.toLowerCase(),
i = 0, k = result.indexOf('' '', i);
while (k -1) {
if (k - i 0) {
result = result.substring(0, i) +
result[i].toUpperCase() +
result.substring(i+1, result.length);
}
i = k + 1;
k = result.indexOf('' '', i);
}
if (result.length - i 1) {
result = result.substring(0, i) +
result[i].toUpperCase() +
result.substring(i+1, result.length);
}
return result;
}
You call this as...
inProperCase(" madam i''m adam ");
..and you get...
" Madam I''m Adam "
Any suggestions for making this a bit more elegant?
BTW You may point me to a "library" with a super-efficient implementation
of this function, but that is not the point. My issue is, how do we update
single characters in a string (or single items in an array) in JavaScript?
Is it something like...
s.wouldYouCareToUpdateThisChar(i, ch); // :D (just kidding, calm
down...)
-or-
s.setCharAt(i, ch);
?
Thanks!
-- Ken
看看这个
http://www.codeproject.com/jscript/propercase.asp
//正确的案例函数(JScript 5.5) +)
函数toProperCase(s)
{
返回s.toLowerCase()。replace(/ ^(。)| \ s (。)/ g,
函数(
take a look at this
http://www.codeproject.com/jscript/propercase.asp
// proper case function (JScript 5.5+)
function toProperCase(s)
{
return s.toLowerCase().replace(/^(.)|\s(.)/g,
function(
1){return
1) { return
1.toUpperCase();}) ;
}
1.toUpperCase(); });
}
这篇关于更新字符串/数组项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!