我试图从一个更大的字符串中提取一个字符串,它得到了a:和a之间的所有东西;
当前的
Str = 'MyLongString:StringIWant;'
期望输出值
newStr = 'StringIWant'
我试图从一个更大的字符串中提取一个字符串,它得到了a:和a之间的所有东西;
当前的
Str = 'MyLongString:StringIWant;'
期望输出值
newStr = 'StringIWant'
当前回答
使用' get_between '实用函数:
get_between <- function(str, first_character, last_character) {
new_str = str.match(first_character + "(.*)" + last_character)[1].trim()
return(new_str)
}
字符串
my_string = 'and the thing that ! on the @ with the ^^ goes now'
用法:
get_between(my_string, 'that', 'now')
结果:
"! on the @ with the ^^ goes
其他回答
使用' get_between '实用函数:
get_between <- function(str, first_character, last_character) {
new_str = str.match(first_character + "(.*)" + last_character)[1].trim()
return(new_str)
}
字符串
my_string = 'and the thing that ! on the @ with the ^^ goes now'
用法:
get_between(my_string, 'that', 'now')
结果:
"! on the @ with the ^^ goes
使用分割()
var s = 'MyLongString:StringIWant;';
var arrStr = s.split(/[:;]/);
alert(arrStr);
arrStr将包含所有由:或; 通过for循环访问每个字符串
for(var i=0; i<arrStr.length; i++)
alert(arrStr[i]);
你也可以试试这个:
var str = 'one:two;three';
str.split(':').pop().split(';')[0]; // returns 'two'
你也可以用这个…
function extractText(str,delimiter){ if (str && delimiter){ var firstIndex = str.indexOf(delimiter)+1; var lastIndex = str.lastIndexOf(delimiter); str = str.substring(firstIndex,lastIndex); } return str; } var quotes = document.getElementById("quotes"); // " - represents quotation mark in HTML <div> <div> <span id="at"> My string is @between@ the "at" sign </span> <button onclick="document.getElementById('at').innerText = extractText(document.getElementById('at').innerText,'@')">Click</button> </div> <div> <span id="quotes"> My string is "between" quotes chars </span> <button onclick="document.getElementById('quotes').innerText = extractText(document.getElementById('quotes').innerText,'"')">Click</button> </div> </div>
我喜欢这个方法:
var str = 'MyLongString:StringIWant;';
var tmpStr = str.match(":(.*);");
var newStr = tmpStr[1];
//newStr now contains 'StringIWant'