在Java中搜索字符串String的字符char和子字符串substring

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)
  •   Java 字符串

    在Java中搜索字符串String的字符char和子字符串substring

    搜索字符串中的字符

    • indexOf(char c):它搜索给定字符串中指定字符的索引。它从字符串的开始到结尾开始搜索(从左到右),并返回相应的索引,否则返回-1。
      注意:如果给定的字符串包含指定字符的多次出现,则它仅返回指定字符的第一次出现的索引。
      句法:

      int indexOf(char c)
      //接受字符作为参数,返回索引 
      //指定字符的第一次出现 
      
    • lastIndexOf(char c):它从字符串的末尾开始向后搜索,并在遇到时返回指定字符的索引。
      句法:

      public int lastIndexOf(char c)
      //接受字符作为参数,返回一个 
      //指定最后一次出现的索引 
      //字符
      
    • IndexOf(char c,int indexFrom):它从字符串中的指定索引处开始向前搜索,并在遇到指定字符时返回相应的索引,否则返回-1。注意:返回的索引必须大于或等于指定的索引。
      句法:

      public int IndexOf(char c,int indexFrom)
      char:要搜索的字符。
      indexfrom:从哪里搜索的整数
      //返回指定字符的索引
      //出现在指定索引中或之后 
      //前进方向
      
    • lastIndexOf(char c,int fromIndex):它从字符串中的指定索引开始向后搜索。并在遇到指定字符时返回相应的索引,否则返回-1。
      注意:返回的索引必须小于或等于指定的索引。
      句法:

      public int lastIndexOf(char c,int fromIndex)
      
    • charAt(int indexNumber):返回给定字符串中指定索引indexNumber处的字符。如果指定的索引号不存在于字符串中,则该方法将引发一个未经检查的异常StringIndexOutOfBoundsException。
      句法:

      char charAt(int indexNumber)
      
      // Java program to illustrate to find a character
      // in the string.
      import java.io.*;
      
      class GFG
      {
        public static void main (String[] args)
        {
          // This is a string in which a character
          // to be searched.
          String str = "GeeksforGeeks is a computer science portal";
      
          // Returns index of first occurrence of character.
          int firstIndex = str.indexOf('s');
          System.out.println("First occurrence of char 's'" +
                             " is found at : " + firstIndex);
      
          // Returns index of last occurrence specified character.
          int lastIndex = str.lastIndexOf('s');
          System.out.println("Last occurrence of char 's' is" +
                             " found at : " + lastIndex);
      
          // Index of the first occurrence of specified char
          // after the specified index if found.
          int first_in = str.indexOf('s', 10);
          System.out.println("First occurrence of char 's'" +
                             " after index 10 : " + first_in);
      
          int last_in = str.lastIndexOf('s', 20);
          System.out.println("Last occurrence of char 's'" +
                           " after index 20 is : " + last_in);
      
          // gives ASCII value of character at location 20
          int char_at = str.charAt(20);
          System.out.println("Character at location 20: " +
                                                   char_at);
      
          // throws StringIndexOutOfBoundsException
          // char_at = str.charAt(50);
        }
      }
      

      输出:

      First occurrence of char 's' is found at : 4
      Last occurrence of char 's' is found at : 28
      First occurrence of char 's' after index 10 : 12
      Last occurrence of char 's' after index 20 is : 15
      Character at location 20: 111

    在字符串中搜索子串

    • 用于在上述字符串中搜索字符的方法也可用于搜索字符串中的子字符串。
      // Java program to illustrate to find a substring
      // in the string.
      import java.io.*;
      
      class GFG
      {
        public static void main (String[] args)
        {
          // This is a string in which a substring
          // is to be searched.
          String str = "GeeksforGeeks is a computer science portal";
      
          // Returns index of first occurrence of substring
          int firstIndex = str.indexOf("Geeks");
          System.out.println("First occurrence of char Geeks"+
                     " is found at : " + firstIndex);
      
          // Returns index of last occurrence
          int lastIndex = str.lastIndexOf("Geeks");
          System.out.println("Last occurrence of char Geeks is"+
                     " found at : " + lastIndex);
      
          // Index of the first occurrence
          // after the specified index if found.
          int first_in = str.indexOf("Geeks", 10);
          System.out.println("First occurrence of char Geeks"+
                     " after index 10 : " + first_in);
      
          int last_in = str.lastIndexOf("Geeks", 20);
          System.out.println("Last occurrence of char Geeks " +
                     "after index 20 is : " + last_in);
        }
      }
      

      输出:

      First occurrence of char Geeks is found at : 0
      Last occurrence of char Geeks is found at : 8
      First occurrence of char Geeks after index 10 : -1
      Last occurrence of char Geeks after index 20 is : 8
    • contains(CharSequence seq):如果String包含指定的char值序列,则返回true,否则返回false。其参数指定要搜索的字符序列,如果seq为null,则会引发NullPointer异常。
      public boolean contains(CharSequence seq)
      

      注意: CharSequence是由String类实现的接口,因此我们在contains()方法中使用string作为参数。

      // Java program to illustrate how to find a substring
      // in the string using contains
      import java.io.*;
      import java.lang.*;
      
      class GFG
      {
        public static void main (String[] args)
        {
          // This is a string in which substring
          // to be searched.
          String test = "software";
      
          CharSequence seq = "soft";
          boolean bool = test.contains(seq);
          System.out.println("Found soft?: " + bool);
      
          // it returns true substring if found.
          boolean seqFound = test.contains("war");
          System.out.println("Found war? " + seqFound);
      
          // it returns true substring if found otherwise
          // return false.
          boolean sqFound = test.contains("wr");
          System.out.println("Found wr?: " + sqFound);
        }
      }
      

      输出:

      Found soft?: true
      Found war? true
      Found wr?: false

    匹配字符串开始和结束

    • boolean startsWith(String str):如果字符串str存在于给定字符串的开始处,则返回true,否则返回false。
    • boolean startsWith(String str,int indexNum):如果字符串str存在于给定字符串的索引indexNum的开始处,则返回true,否则返回false。
    • boolean endsWith(String str):如果字符串str存在于给定字符串的结尾处,则返回true,否则返回false。
      // Java program to illustrate to match
      // of start and end of a substring
      import java.io.*;
      
      class GFG
      {
        public static void main (String[] args)
        {
          // This is a string in which substring
          // is to be searched.
          String str = "GeeksforGeeks is a computer science portal";
      
          System.out.println(str.startsWith("Geek"));
          System.out.println(str.startsWith("is", 14));
          System.out.println(str.endsWith("port"));
        }
      }
      

      输出:

      true
      true
      false
  •   Java 字符串
  •   本文标题:在Java中搜索字符串String的字符char和子字符串substring - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/3994.html

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注

    更多阅读