gpt4 book ai didi

java - 在字符串中查找子字符串的最佳方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:31:25 25 4
gpt4 key购买 nike

我在尝试搜索字符串中的子字符串时遇到问题。该子字符串可能在也可能不在字符串中。

str = "hello how are you?"
substr = "how are"

我知道是否可以完成的两种方法是:

  1. string.indexOf("怎么样")
  2. 正则表达式

但是,还有其他“优化”的方式吗?你会怎么做?

Ruby 能否提供更好的答案?由于我们使用 jRuby,所以答案可以是 Ruby 或 Java。

最佳答案

在 Ruby 中,使用 String#include? 方法:

str = "hello how are you?"
substr = "how are"
str.include? substr

返回 true

关于java - 在字符串中查找子字符串的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3876246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com