如何只将字符串拆分一次,即将1|Ceci n'est pas une pipe: | Oui
解析为:["1", "Ceci n'est pas une pipe: | Oui"]
?
分裂的限制似乎没有帮助......
答案 0 :(得分:100)
您希望使用String.indexOf('|')
来获取第一次出现'|'的索引。
var i = s.indexOf('|');
var splits = [s.slice(0,i), s.slice(i+1)];
答案 1 :(得分:71)
这不是一个很好的方法,但效率很高:
var string = "1|Ceci n'est pas une pipe: | Oui";
var components = string.split('|');
alert([components.shift(), components.join('|')]);
答案 2 :(得分:12)
您可以使用:
var splits = str.match(/([^|]*)\|(.*)/);
splits.shift();
正则表达式将字符串拆分为两个匹配的组(括号),即第一个|之前的文本以及后面的文字。然后,我们shift
结果去除整个字符串匹配(splits[0]
)。
答案 3 :(得分:6)
一个衬里和imo,更简单:
var str = 'I | am super | cool | yea!';
str.split('|').slice(1).join('|');
这将返回“am super | cool | yea!”
答案 4 :(得分:3)
试试这个:
function splitOnce(input, splitBy) {
var fullSplit = input.split(splitBy);
var retVal = [];
retVal.push( fullSplit.shift() );
retVal.push( fullSplit.join( splitBy ) );
return retVal;
}
var whatever = splitOnce("1|Ceci n'est pas une pipe: | Oui", '|');
答案 5 :(得分:3)
如果字符串不包含分隔符@ NickCraver的解决方案仍将返回两个元素的数组,第二个是空字符串。我更喜欢与分裂相匹配的行为。也就是说,如果输入字符串不包含分隔符,则只返回一个包含单个元素的数组。
var splitOnce = function(str, delim) {
var components = str.split(delim);
var result = [components.shift()];
if(components.length) {
result.push(components.join(delim));
}
return result;
};
splitOnce("a b c d", " "); // ["a", "b c d"]
splitOnce("a", " "); // ["a"]
答案 6 :(得分:1)
就像迄今为止大多数答案一样邪恶:
var splits = str.split('|');
splits.splice(1, splits.length - 1, splits.slice(1).join('|'));
答案 7 :(得分:0)
除了其他地方的商品之外,另一种简短的方法是使用replace()
限制你的优势。
var str = "1|Ceci n'est pas une pipe: | Oui";
str.replace("|", "aUniquePhraseToSaySplitMe").split("aUniquePhraseToSaySplitMe");
正如@sreservoir在评论中指出的那样,这个独特的短语必须是真正独一无二的 - 它不能在你运行这个分裂的源头中,或者你将把字符串拆分成比你想要的更多的部分。正如他所说,如果你是针对用户输入运行它(即在浏览器中输入),那么一个不可打印的角色可能会这样做。
答案 8 :(得分:0)
这个有点长,但它的作用就像我认为极限应该:
function split_limit(inString, separator, limit){
var ary = inString.split(separator);
var aryOut = ary.slice(0, limit - 1);
if(ary[limit - 1]){
aryOut.push(ary.slice(limit - 1).join(separator));
}
return aryOut;
}
console.log(split_limit("1|Ceci n'est pas une pipe: | Oui","|", 1));
console.log(split_limit("1|Ceci n'est pas une pipe: | Oui","|", 2));
console.log(split_limit("1|Ceci n'est pas une pipe: | Oui","|", 3));
console.log(split_limit("1|Ceci n'est pas une pipe: | Oui","|", 7));
https://jsfiddle.net/2gyxuo2j/
零限制返回有趣的结果,但在效率的名义,我遗漏了支票。如果需要,可以将其添加为函数的第一行:
if(limit < 1) return [];
答案 9 :(得分:0)
ES6语法允许使用其他方法:
function splitOnce(s, on) {
[first, ...rest] = s.split(on)
return [first, rest.length > 0? rest.join(on) : null]
}
还可以通过返回null而不是空字符串来处理没有|
的字符串的可能性,
splitOnce("1|Ceci n'est pas une pipe: | Oui", "|")
>>> ["1", "Ceci n'est pas une pipe: | Oui"]
splitOnce("Celui-ci n'a pas de pipe symbol!", "|")
>>> ["Celui-ci n'a pas de pipe symbol!", null]
Pas de pipe?没有空!
我添加此回复的主要目的是使我可以对竖线符号进行双关语,同时也可以展示es6语法-令人惊讶的是还有多少人不使用它...
答案 10 :(得分:0)
如果您想使用“管道”,则reduce
是您的朋友
const separator = '|'
jsonNode.split(separator)
.reduce((previous, current, index) =>
{
if (index < 2) previous.push(current)
else previous[1] += `${separator}${current}`
return previous
}, [])
.map((item: string) => (item.trim()))
.filter((item: string) => (item != ''))
答案 11 :(得分:0)
更有效的方法:
alpha = [356.37, 359.80, 357.14, 359.18, 350.97, 347.35, 348.98, 351.80, 2.74, 354.55, 354.13, 357.82, 3.86, 2.42, 3.57, 1.57, 357, 358]
#take derivative of list of angles alpha
alpha_diff = [0, np.diff(alpha)]
#iterate over each item in alpha_diff and when it finds jumps <-360 you will add 360 until it finds the next jump
for i in alpha_diff:
if alpha_diff[i]<-300:
while alpha_diff[i]<100:
alpha[i] = alpha[i]+360
#similarly if finds jumps > 360 you will subtract 360 until it finds the next jump
if alpha_diff[i]>300:
while alpha_diff[i]<100:
alpha[i] = alpha[i]-360
else:
alpha[i] = alpha
答案 12 :(得分:0)
这是一个老问题,但如果你需要遍历字符串, 并且有多个分隔符,使用正则表达式来匹配你的情况, 像这样:
let exampleRegexp = />|<|=|\||(?:and|not|etc)/
let strings = ["left | middle | right", "yes and not yes"]
function splitOnce(str, regexp){
let check = regexp.exec(str)
let tail = str.slice(check.index + check.toString().length)
let head = str.substring(0, check.index)
return [head, tail]
}
for(let str of strings){
let [head, tail] = splitOnce(str, exampleRegexp)
console.log(head + ":::" + tail)
}
答案 13 :(得分:-1)
使用javascript正则表达式功能并获取第一个捕获的表达式。
RE可能看起来像/^([^|]*)\|/
。
实际上,如果由于javascript正则表达式贪婪而验证字符串的格式是这样的,那么你只需要/[^|]*/
。