找到字符串中特定字符的出现次数。

huangapple go评论78阅读模式
英文:

find the occurrence of a particular character in a string

问题

我需要检查条件,如果字符串中包含介于1和7之间(包括1和7),则返回true,否则返回false。

我的代码如下:

const [cartOptions, setCartOptions] = useState(null);

useEffect(() => {
  if (trolleyName) {
    trolleyFinder(trolleyName);
  }
}, [trolleyName]);

const trolleyFinder = (trolleyName) => {
  if (
    trolleyName &&
    (trolleyName.includes("1") ||
      trolleyName.includes("2") ||
      trolleyName.includes("3") ||
      trolleyName.includes("4") ||
      trolleyName.includes("5") ||
      trolleyName.includes("6") ||
      trolleyName.includes("7"))
  ) {
    setCartOptions("1");
  } else {
    setCartOptions("2");
  }
};

在这段代码中,我们检查了trolleyName是否包含数字1到7中的任何一个,如果包含则将cartOptions设置为"1",否则设置为"2"。

请注意,这段代码应该正常工作,但如果您没有得到预期的结果,可能需要检查一下trolleyName的值是否正确或其他相关代码是否有问题。

英文:

I have strings like "Galley1","Galley2","Galley3","Galley4","Galley5","Galley6" upto "Galley20"

i need to check a condition which returns true if the string has number between 1 and 7 both included else return a false
my code is as follows

const [cartOptions,setCartOptions]=useState(null);
useEffect(() => {

  if(trolleyName){
    trolleyFinder(trolleyName);
  }
}, [trolleyName])

const trolleyFinder=(trolleyName)=>{
if(trolleyName&&trolleyName.includes("1"||"2"||"3"||"4"||"5"||"6"||"7")){
  setCartOptions("1")
}
else{
  setCartOptions("2")
}
}

here trolleyName is a prop which changes when i click a button and when i close the page i am making the cartOptions as null again.but i am not getting the desired result.
what else could be done

答案1

得分: 0

trolleyName.includes("1" || "2" || "3" || "4" || "5" || "6" || "7")

这段代码不会起作用,因为 || 运算符会计算并返回遇到的第一个真值,而在这种情况下是 1。所以你的代码与 trolleyName.includes("1") 相同。

要检查一个字符串是否包含某个特定范围内的数字(在这种情况下是1-7),你可以使用正则表达式的 .test() 方法,检查是否匹配 1-7 之间的数字:

if (trolleyName && /Galley[1-7]$/.test(trolleyName))

这里的 Galley[1-7]$ 表示匹配文本 "Galley" 后面跟着1到7之间的数字(然后是字符串的结尾 $)。

话虽如此,如果你完全控制 trolleyName,我建议将其制作成一个包含数字作为单独属性的对象。这样,你可以直接针对该属性进行检查。此外,正如@Wimanicesir建议的那样,如果你最终使用了一个对象,你可以设置 cartOption 值,这样你就不需要担心条件判断了。


<details>
<summary>英文:</summary>

```js
trolleyName.includes(&quot;1&quot;||&quot;2&quot;||&quot;3&quot;||&quot;4&quot;||&quot;5&quot;||&quot;6&quot;||&quot;7&quot;)

This won't work because the || operator evaluates to the first truthy value it comes across, which in this case is &quot;1&quot;. So your code is the same as doing trolleyName.includes(&quot;1&quot;)

To check if a string contains a number between a certain range (in this case 1-7) you can use .test() on a regular expression, that checks for numbers between 1-7:

if(trolleyName &amp;&amp; /Galley[1-7]$/.test(trolleyName))

Here Galley[1-7]$ means match against the text "Galley" followed by numbers between 1 and 7 inclusive (and then the end of the string $).

With that being said, if you have full control over trolleyName, I suggest making it an object that contains the number as a separate property. That way you can easily check against this property directly. Furthermore, as @Wimanicesir suggests, if you end up using an object, you can set the cartOption value, which then means you don't need to worry about your conditional.

huangapple
  • 本文由 发表于 2023年5月17日 19:17:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76271493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定