Lui*_*ixv 6 java string substring pattern-matching
嗨,我必须计算一个给定的字符串是否是一个更大的字符串的子字符串.例如
String str = "Hallo my world";
String substr = "my"
Run Code Online (Sandbox Code Playgroud)
方法"contains"应返回true,因为str包含substr(否则为false).
我在String类中寻找类似"contains"的东西,但我找不到它.我想唯一的解决方案是使用模式匹配.如果这是更好(最便宜)的方式吗?
谢谢!
Joa*_*uer 22
这里是一个contains()方法!它是在Java 1.5中引入的.如果您使用的是早期版本,则可以轻松地将其替换为:
str.indexOf(substr) != -1
Run Code Online (Sandbox Code Playgroud)
String str="hello world";
System.out.println(str.contains("world"));//true
System.out.println(str.contains("world1"));//false
Run Code Online (Sandbox Code Playgroud)