[英]How to remove a specific section of a string after the last occurrence of a specific character?
I have one string 我有一串
"Opportunity >> Source = Email >> Status = New >> Branch = Mumbai"
Now, I want to chop off above string from last occurrence of >>
. 现在,我想从最后出现的
>>
截断字符串。 I mean, I want the result string should be like this 我的意思是,我希望结果字符串应该像这样
"Opportunity >> Source = Email >> Status = New"
Now, I am using various jquery/javascript functions like split()
, reverse()
, join()
, indexOf()
to remove the rest of the string from the last occurrence of >>
, like this 现在,我正在使用各种jquery / javascript函数,例如
split()
, reverse()
, join()
, indexOf()
,从最后一次>>
删除字符串的其余部分,像这样
var test = "Opportunity >> Source = Email >> Status = New >> Branch = Mumbai";
var count = test.split("").reverse().join("").indexOf('>>');
var string = test.substring(0, test.length - count-2);
Using this, I am able to get the desired result. 使用此方法,我可以获得所需的结果。 But I assume there must be some other easier way than this to achieve this using jquery/javascript.
但是我认为除了jquery / javascript之外,还必须有其他简便的方法。
Please try this: 请尝试以下方法:
"Opportunity >> Source = Email >> Status = New >> Branch = Mumbai".split(" >> ").slice(0, -1).join(" >> ")
var str = "Opportunity >> Source = Email >> Status = New >> Branch = Mumbai"; console.log(str.split(" >> ").slice(0, -1).join(" >> "));
You can simply use lastIndexOf method of String 您可以简单地使用String的lastIndexOf方法
var input = "Opportunity >> Source = Email >> Status = New >> Branch = Mumbai"; console.log( input.substring(0, input.lastIndexOf( ">>" )) )
You can get the lastindex of >>
and then get substring from beginning of string to last position of element >>
in it : 您可以获取
>>
的lastindex,然后从字符串的开头到元素>>
最后位置获取子字符串:
var str = "Opportunity >> Source = Email >> Status = New >> Branch = Mumbai" var lastindex = str.lastIndexOf(">>"); if (lastindex != -1){ alert(str.substring(0, lastindex-1 )); }
Javascript has lastIndexOf() method . Javascript具有lastIndexOf()方法。 You can find last occurance with it.
您可以找到它的最后一次出现。 Then remove rest of the string easily.
然后轻松移除其余的字符串。 Example:
例:
var test = "Opportunity >> Source = Email >> Status = New >> Branch = Mumbai";
var finIndex = test.lastIndexOf('>>');
var string = test.substring(0, finIndex);
You can splice
it off: 你可以
splice
其关闭:
var arr = test.split(">>");
arr = arr.splice(arr.length - 1, 1);
string = test.join(">>");
One more option: use pop()
另一个选择:使用
pop()
var src = "Opportunity >> Source = Email >> Status = New >> Branch = Mumbai"
var arr = src.split(">>");
arr.pop();
var s = arr.join(">>");
You can use regex in .replace()
to removing extra part of string. 您可以在
.replace()
使用regex删除字符串的多余部分。 Regex select last part of string ( >> Branch = Mumbai
) and repalace it with empty. 正则表达式选择字符串的最后一部分(
>> Branch = Mumbai
)并用空白代替它。 You can see it in demo 你可以在演示中看到它
var text = "Opportunity >> Source = Email >> Status = New >> Branch = Mumbai"; console.log(text.replace(/>>[^>]+$/g, ""));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.