Javascript javascript子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1989009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:16:31  来源:igfitidea点击:

javascript substring

javascriptstringsubstring

提问by akula1001

the most darndest thing! the following code prints out 'llo' instead of the expected 'wo'. i get such surprising results for a few other numbers. what am i missing here?

最可恶的事情!以下代码打印出“llo”而不是预期的“wo”。对于其他一些数字,我得到了如此惊人的结果。我在这里错过了什么?

alert('helloworld'.substring(5, 2));

回答by Christoph

You're confusing substring()and substr(): substring()expects two indices and not offset and length. In your case, the indices are 5 and 2, ie characters 2..4 will be returned as the higher index is excluded.

你很困惑substring()并且substr():substring()需要两个索引而不是偏移量和长度。在您的情况下,索引是 5 和 2,即字符 2..4 将被返回,因为更高的索引被排除在外。

回答by Chirag

You have three options in Javascript:

在 Javascript 中有三个选项:

//slice
//syntax: string.slice(start [, stop])
"Good news, everyone!".slice(5,9); // extracts 'news'

//substring 
//syntax: string.substring(start [, stop])
"Good news, everyone!".substring(5,9); // extracts 'news'

//substr
//syntax: string.substr(start [, length])
"Good news, everyone!".substr(5,4); // extracts 'news'

回答by Pekka

Check the substringsyntax:

检查substring语法

substring(from, to)

fromRequired. The index where to start the extraction. First character is at index 0

toOptional. The index where to stop the extraction. If omitted, it extracts the rest of the string

子串(从,到)

必需。开始提取的索引。第一个字符位于索引 0

可选。停止提取的索引 。如果省略,则提取字符串的其余部分

I'll grant you it's a bit odd. Didn't know that myself.

我承认这有点奇怪。我自己不知道。

What you want to do is

你想做的是

alert('helloworld'.substring(5, 7));

回答by AutomatedTester

alert('helloworld'.substring(5, 2));

The code above is wrong because the first value is the start point to the end point.E.g move from char 5 which is oand go to char 2 which is the lso will get lloSo you have told it to go backwards.

上面的代码是错误的,因为第一个值是起点到终点。例如,从字符 5 移动o到字符 2,l这样就会得到llo所以你已经告诉它向后走。

What yuou want is

你想要的是

alert('helloworld'.substring(5, 7));

回答by Alexandru Diacov

See syntax below:

请参阅下面的语法:

str.substring(indexA, [indexB])

If indexA > indexB, the substring()function acts as if arguments were reversed.

如果indexA > indexBsubstring()函数就像参数被反转一样。

Consider documentation here: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/substring

考虑这里的文档:https: //developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/substring

回答by Nayas Subramanian

This is What i have done,

这是我所做的,

var stringValue = 'Welcome to India';

// if you want take get 'India' 
// stringValue.substring(startIndex, EndIndex)
   stringValue.substring(11, 16); // O/p 'India'