我在尝试将不同的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次移除:
":2 E/4 :2 G/4 |"
3次移除:
":2 E/4 "
我该怎么做?
答案 0 :(得分:3)
使用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
答案 1 :(得分:1)
您可以使用lastindexof()获取":"
的最后一次出现,然后使用substring()
var str = ":2 E/4 :2 G/4 |:2 G/4 :2 F/4";
var n = str.lastIndexOf(":");
console.log(str.substring(0, n));
答案 2 :(得分:1)
试试这个。
<!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>
答案 3 :(得分:1)
一个选项,虽然比许多选项稍微冗长,但是如下:
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
function removeLastNSections(opts) {
var settings = {
'text': null,
'separator': ':',
'n': 1
},
haystack,
numSections;
Object.keys(opts || {}).forEach(function(key) {
settings[key] = opts[key];
});
if (settings.text) {
haystack = settings.text.split(settings.separator);
numSections = haystack.length;
return haystack.slice(0, numSections - settings.n).join(settings.separator);
}
return '';
}
console.log(
removeLastNSections({
'text': ":2 E/4 :2 G/4 |:2 G/4 :2 F/4"
})
);
console.log(
removeLastNSections({
'text': ":2 E/4 :2 G/4 |:2 G/4 :2 F/4",
'n': 1
})
);
console.log(
removeLastNSections({
'text': ":2 E/4 :2 G/4 |:2 G/4 :2 F/4",
'n': 2
})
);
console.log(
removeLastNSections({
'text': ":2 E/4 :2 G/4 |:2 G/4 :2 F/4",
'n': 3
})
);
参考文献: