在 angular 4 中搜索包含多个字符的字符串中的字符
search for character in string contains multiple characters in angular 4
我有两个字符串是这样的 "L7 - LO" 和 "% L7 - LO"..
如果字符串只包含“-”我需要根据这个做一些处理,如果字符串包含这两个字符“-”,“%”我只需要考虑%而忽略“-”字符。
为了这个目的,我在下面做了这个
if (this.selectedSources[formula].Value.indexOf('%') == -1) {
this.formulaType = "percent"
}
else if (this.selectedSources[formula].Value.indexOf('-') == -1) {
this.formulaType = "diff";
}
但有些上面的代码不起作用..
如果angular中有两个字符,请问如何只区分一个字符
如果条件应该改变。休息看起来不错 -
if (this.selectedSources[formula].Value.indexOf('%') !== -1) {
this.formulaType = "percent"
}
else if (this.selectedSources[formula].Value.indexOf('-') !== -1) {
this.formulaType = "diff";
}
如果您需要测试字符串中的 %
和 -
,我会使用 RegEx:
const regex = new RegEx(/.*[%]{1}.*[-]{1}/);
this.formulaType = regex.test(this.selectedSources[formula].Value) ? 'percent' : 'diff';
否则,您可以使用 .indexOf('%')
:
this.formulaType = this.selectedSources[formula].Value.indexOf('%') !== -1
? 'percent'
: 'diff';
我有两个字符串是这样的 "L7 - LO" 和 "% L7 - LO"..
如果字符串只包含“-”我需要根据这个做一些处理,如果字符串包含这两个字符“-”,“%”我只需要考虑%而忽略“-”字符。
为了这个目的,我在下面做了这个
if (this.selectedSources[formula].Value.indexOf('%') == -1) {
this.formulaType = "percent"
}
else if (this.selectedSources[formula].Value.indexOf('-') == -1) {
this.formulaType = "diff";
}
但有些上面的代码不起作用..
如果angular中有两个字符,请问如何只区分一个字符
如果条件应该改变。休息看起来不错 -
if (this.selectedSources[formula].Value.indexOf('%') !== -1) {
this.formulaType = "percent"
}
else if (this.selectedSources[formula].Value.indexOf('-') !== -1) {
this.formulaType = "diff";
}
如果您需要测试字符串中的 %
和 -
,我会使用 RegEx:
const regex = new RegEx(/.*[%]{1}.*[-]{1}/);
this.formulaType = regex.test(this.selectedSources[formula].Value) ? 'percent' : 'diff';
否则,您可以使用 .indexOf('%')
:
this.formulaType = this.selectedSources[formula].Value.indexOf('%') !== -1
? 'percent'
: 'diff';