当前位置:   article > 正文

Java String类的常用方法(字符串查找)_java string find

java string find

No.  

方法名称

类型

描述

1

public boolean contains(String str)

普通

判断指定内容是否存在

2

public int indexOf(String str)

普通

右前向后查找指定字符串位置,如果超找到了则返回(第一个字母)位置索引,如果找不到返回-1

3

public int indexOf(String str,int fromIndex)

普通

由指定位置从前向后查找指定字符串的位置,找不到返回-1

4

public int lastIndexOf(String str)

普通

由后向前查找字符串位置,找不到返回-1

5

public int lastIndexOf(String str,int fromIndex)

普通

从指定位置由后向前查找字符串位置,找不到返回-1

6

public boolean startsWith(String prefix)

普通

判断是否以指定的字符开头

7

public boolean startsWith(String prefix,int toffset)

普通

从指定位置开始判断是否以指定的字符开头

8

public boolean endWith(String suffix)

普通

判断是否以指定的字符串结尾



  1. public class Demo {
  2. public static void main(String[] args) {
  3. String str = "helloworld";
  4. System.out.println("\"world\"所在的索引:" + str.indexOf("world"));
  5. System.out.println("第一个\"l\"所在的索引:" + str.indexOf("l"));
  6. System.out.println("从索引是5开始超找\"l\"所在的位置:" + str.indexOf("l", 5));
  7. }
  8. }


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

  1. public class Demo {
  2. public static void main(String[] args) {
  3. String str = "helloworld";
  4. if (str.contains("world")) {
  5. System.out.println("查找成功");
  6. }
  7. }
  8. }

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

  1. public class Demo {
  2. public static void main(String[] args) {
  3. String str = "$$@@hello***";
  4. System.out.println("是否以$$开头:" + str.startsWith("$$"));
  5. System.out.println("在索引为2的位置是否为@:" + str.startsWith("@@", 2));
  6. System.out.println("是否以*结尾:" + str.endsWith("*"));
  7. }
  8. }

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/article/detail/45574
推荐阅读
相关标签
  

闽ICP备14008679号