简体   繁体   English

如何从字符串中删除符号以及此符号之后的所有内容

[英]How to remove symbol and everything after this symbol from a string

Guys i want to know how can i remove a symbol and everything after that symbol from a string with jQuery or JavaScript. 伙计们,我想知道如何使用jQuery或JavaScript从字符串中删除符号以及该符号之后的所有内容。

Here is my code: 这是我的代码:

var optionValueText = $.trim($('[data-divNumber="on"] :selected').text());

The variable optionValueText is printing results like My first result +24.00 euros 变量optionValueText正在打印结果,例如“ My first result +24.00 euros

So what i want to do is to remove everyting after the + plus symbol including the plus symbol itself. 所以我想做的是删除+加号后的所有内容,包括加号本身。

So for final result of my example i want to receive a result like this My first result and that is it! 因此,对于我的示例的最终结果,我想收到这样的结果My first result就是这样!

How can i make it? 我该怎么做?

Thanks in advance! 提前致谢!

Working Fiddle 工作小提琴

Use 采用

optionValueText = optionValueText.substring(0, optionValueText.indexOf('+'));
alert(optionValueText);

Explanation : Find index(character position) of + and take substring from 0 till that index of + . 说明:查找+的索引(字符位置),并从0到+的该索引取子字符串。

For more info read here 欲了解更多信息,请点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM