Common methods of the Java String class (string search)

No. 1 Method name type description

1      public boolean contains(String str) 普通     判断指定内容是否存在

2 public int indexOf(String str) Normally search the specified string position forward and backward, if it is found, it will return (the first letter) position index, if it is not found, return -1

3 public int indexOf(String str,int fromIndex) Normally search the position of the specified string from front to back from the specified position, return -1 if not found

4 public int lastIndexOf(String str) Normally search string position from back to front, return -1 if not found

5 public int lastIndexOf(String str,int fromIndex) Normally search for the position of the string from the specified position backward to forward, return -1 if not found

6 public boolean startsWith(String prefix) Ordinarily judge whether it starts with the specified character

7 public boolean startsWith(String prefix,int toffset) Normally starting from the specified position to determine whether it starts with the specified character

8 public boolean endWith(String suffix) Ordinarily judge whether to end with the specified string

public class Demo { public static void main(String[] args) { String str = "helloworld"; System.out.println(index where ""world" is located:" + str.indexOf("world")); System. out.println("The index of the first "l":" + str.indexOf("l")); System.out.println("Find the position of "l" starting from index 5:" + str.indexOf(“l”, 5)); } }






====================================================

public class Demo {
public static void main(String[] args) {
String str = “helloworld”;
if (str.contains(“world”)) {
System.out.println(“查找成功”);
}
}
}

====================================================

public class Demo {
public static void main(String[] args) {
String str = “ @ @ h e l l o ∗ ∗ ∗ " ; S y s t e m . o u t . p r i n t l n ( " 是 否 以 @@hello***"; System.out.println("是否以 @@hello";S Y S T E m . O U T . P R & lt I n- T L n- ( " is NO in the beginning:" + str.startsWith ( "$$"));
System.out.println ( "in the index 2 Is the position @:" + str.startsWith("@@", 2));
System.out.println("Whether itends with:" + str.endsWith("”));
}
}

Guess you like

Origin blog.csdn.net/weixin_42041819/article/details/100176795