javascript 在javascript中的单词前后截断字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21326193/
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
truncating a string after and before a word in javascript
提问by user2882721
How do I truncate a string after or before a pattern?
如何在模式之后或之前截断字符串?
Say if I have a string "abcdef"
I need to truncate everything after "abc"
so the output will be:
假设我有一个字符串,"abcdef"
我需要在之后截断所有内容,"abc"
因此输出将是:
def
def
and if i say truncate before "def"
the output should be:
如果我"def"
在输出之前说 truncate应该是:
abc
abc
Below is the code that I tried
下面是我试过的代码
var str1 = "abcdefgh";
var str2 = str1.substr(str1.indexOf("abc"), str1.length);
console.log(str2);
I didn't get the output.
我没有得到输出。
I'm stuck here any help will be much appreciated.
我被困在这里任何帮助将不胜感激。
回答by hemanth
You need to pass length of "abc" as the 2nd argument in substr method
您需要传递“abc”的长度作为 substr 方法中的第二个参数
var str1 = "abcdefgh";
var pattern = "abc";
var str2 = str1.substr(str1.indexOf(pattern), pattern.length); <-- check this line
console.log(str2);
However above code might return unexpected results for patterns which are not present in the string.
但是,对于字符串中不存在的模式,上面的代码可能会返回意外的结果。
var str1 = "abcdefgh";
var pattern = "bcd";
var str2 = "";
if(str1.indexOf(pattern)>=0) //if a pattern is not present in the source string indexOf method returns -1
{
//to truncate everything before the pattern
//outputs "efgh"
str2 = str1.substr(str1.indexOf(pattern)+pattern.length, str1.length);
console.log("str2: "+str2);
// if you want to truncate everything after the pattern & pattern itself
//outputs "a"
str3 = str1.substr(0, str1.indexOf(pattern));
console.log("str3: "+str3);
}
回答by avetisk
var str = "sometextabcdefine";
var pattern = "abc";
var truncateBefore = function (str, pattern) {
return str.slice(str.indexOf(pattern) + pattern.length);
};
var truncateAfter = function (str, pattern) {
return str.slice(0, str.indexOf(pattern));
}
console.log(truncateBefore(str, pattern)); // "define"
console.log(truncateAfter(str, pattern)); // "sometext"
回答by David
How about something like this:
这样的事情怎么样:
function truncateAfter(original, pattern) {
return original.substring(0, original.indexOf(pattern) + pattern.length);
}
What this does is find the first index of the pattern you're looking for, and return a substring of the original string that starts at the beginning and ends after the first instance of the pattern.
它的作用是找到您要查找的模式的第一个索引,并返回原始字符串的一个子字符串,该子字符串从该模式的第一个实例的开头开始并在该模式的第一个实例之后结束。
Example Usage:
示例用法:
truncateAfter('dabcdefghi', 'abc');
>> 'dabc'
If instead you want to truncate the output before and after the pattern you're looking for, would just checking if the pattern is in the string and then using the pattern as the output be what you're looking for?
相反,如果您想在您正在查找的模式之前和之后截断输出,是否只需检查该模式是否在字符串中,然后使用该模式作为您正在寻找的输出?
function truncate(original, pattern) {
if (original.indexOf(pattern) != -1) {
return pattern;
}
}
Example Usage:
示例用法:
truncate('dabcdefghi', 'abc');
>> 'abc'
回答by Furquan Khan
Please see the below code:
请看下面的代码:
var str1 = "abcdefgh";
var str2 = str1.substr(str1.indexOf("abc")+3, str1.length);
alert(str2);
You were correct but one thing you missed is doing +3 in the indexOf. the indexOf("abc") would return 0 which in turn will give you thw whole string again.
你是对的,但你错过的一件事是在 indexOf 中做 +3。indexOf("abc") 将返回 0,这反过来又会给你整个字符串。
Or check out this fiddle link:
或查看此小提琴链接: