从字符串末尾使用JavaScript进行字符串操作
我在尝试将不同的JavaScript字符串操作解决方案应用于我的特定问题时遇到了困难:从字符串末尾使用JavaScript进行字符串操作,javascript,jquery,html,string,Javascript,Jquery,Html,String,我在尝试将不同的JavaScript字符串操作解决方案应用于我的特定问题时遇到了困难: string str = ":2 E/4 :2 G/4 |:2 G/4 :2 F/4" 从字符串的末尾,我想一直删除找到的第一个: 1删除: ":2 E/4 :2 G/4 |:2 G/4 " ":2 E/4 :2 G/4 |" ":2 E/4 " 2次删除: ":2 E/4 :2 G/4 |:2 G/4 " ":2 E/4 :2 G/4 |" ":2 E/4 " 3次删除: ":2 E/4 :2
string str = ":2 E/4 :2 G/4 |:2 G/4 :2 F/4"
从字符串的末尾,我想一直删除找到的第一个:
1删除:
":2 E/4 :2 G/4 |:2 G/4 "
":2 E/4 :2 G/4 |"
":2 E/4 "
2次删除:
":2 E/4 :2 G/4 |:2 G/4 "
":2 E/4 :2 G/4 |"
":2 E/4 "
3次删除:
":2 E/4 :2 G/4 |:2 G/4 "
":2 E/4 :2 G/4 |"
":2 E/4 "
如何执行此操作?您可以使用获取上次出现的“:”
,然后使用
var str=“:2e/4:2g/4 |:2g/4:2f/4”;
var n=str.lastIndexOf(“:”);
log(str.substring(0,n))
使用String.split
、Array.slice
和Array.join
函数的解决方案:
/**
* @param string str Input string with 'colons'
* @param number removals Number of removals
* @returns string
*/
var sliceToColon = function (str, removals) {
var parts = str.split(":");
return parts.slice(0, -removals).join(":");
}
console.log(sliceToColon(":2 E/4 :2 G/4 |:2 G/4 :2 F/4", 1)); // :2 E/4 :2 G/4 |:2 G/4
console.log(sliceToColon(":2 E/4 :2 G/4 |:2 G/4 :2 F/4", 2)); // :2 E/4 :2 G/4 |
console.log(sliceToColon(":2 E/4 :2 G/4 |:2 G/4 :2 F/4", 3)); // :2 E/4
试试这个
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction(':2 E/4 :2 G/4 |:2 G/4 :2 F/4','2')">Click</button>
<script>
function myFunction(str,n) {
var x,y;
for (i = 0; i < n; i++) {
x=str.lastIndexOf(":");
y=str.substr(0,str.lastIndexOf(":"));
str= y;
}
document.getElementById("demo").innerHTML = y;
}
</script>
</body>
</html>
点击
函数myFunction(str,n){
变量x,y;
对于(i=0;i
一个选项,尽管比许多选项更详细,但如下所示:
function removeLastNSections(opts) {
// the default settings,
// text: String, must be user-supplied;
// the string to be split and from
// which 'sections' should be removed.
// separator: String,the character upon which
// string should be split.
// n: Number, or numeric String (4 or '4'),
// the number of 'sections' to remove.
var settings = {
'text' : null,
'separator' : ':',
'n' : 1
},
haystack,
numSections;
// updating the defaults with user-supplied values:
Object.keys(opts || {}).forEach(function(key){
settings[key] = opts[key];
});
// if settings.text exists:
if (settings.text) {
// we split the user-supplied string of text on
// occurrences of the separator:
haystack = settings.text.split( settings.separator );
// an integer for the length of the haystack array:
numSections = haystack.length;
// we slice the haystack array, taking all elements from
// the first (index: 0) to the index resulting from the
// sum of the number of elements in the array minus the
// number of 'sections' to remove:
return haystack.slice(0, numSections - settings.n )
// and join those array-elements back into a
// a string by joining them together with the
// separator:
.join( settings.separator );
}
return '';
}
console.log(
removeLastNSections({
'text' : ":2 E/4 :2 G/4 |:2 G/4 :2 F/4"
})
); // => :2 E/4 :2 G/4 |:2 G/4
console.log(
removeLastNSections({
'text' : ":2 E/4 :2 G/4 |:2 G/4 :2 F/4",
'n': 1
})
); // => :2 E/4 :2 G/4 |:2 G/4
console.log(
removeLastNSections({
'text' : ":2 E/4 :2 G/4 |:2 G/4 :2 F/4",
'n' : 2
})
); // => :2 E/4 :2 G/4 |
console.log(
removeLastNSections({
'text' : ":2 E/4 :2 G/4 |:2 G/4 :2 F/4",
'n' : 3
})
); // => :2 E/4
函数移除设置(opts){
变量设置={
“文本”:null,
“分隔符”:“:”,
“n”:1
},
干草堆,
numSections;
Object.keys(opts |{}).forEach(function(key){
设置[键]=选择[键];
});
if(settings.text){
haystack=settings.text.split(settings.separator);
numSections=haystack.length;
返回haystack.slice(0,numSections-settings.n).join(settings.separator);
}
返回“”;
}
console.log(
切除({
‘文本’:“:2E/4:2G/4 |:2G/4:2F/4”
})
);
console.log(
切除({
‘文本’:“:2E/4:2G/4 |:2G/4:2F/4”,
“n”:1
})
);
console.log(
切除({
‘文本’:“:2E/4:2G/4 |:2G/4:2F/4”,
n:2
})
);
console.log(
切除({
‘文本’:“:2E/4:2G/4 |:2G/4:2F/4”,
n:3
})
);代码>你能提供给我们你尝试过的代码吗?酷而简单!真不敢相信我居然没想到这个,谢谢!我将实现类似的东西,这实际上帮助我思考我将面临的未来问题!谢谢谢谢你,大卫!我很喜欢冗长的解释,没什么大不了的。