java indexOf() lastIndexOf() 字符串查找函数

利用indexOf()匹配字符串

返回 String 对象内第一次出现子字符串的字符位置。

strObj.indexOf(subString[, startIndex])
参数
strObj
必选项。String 对象或文字。
subString
必选项。要在 String 对象中查找的子字符串。
starIndex
可选项。该整数值指出在 String 对象内开始查找的索引。如果省略,则从字符串的开始处查找。
说明
indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回 -1。

如果 startindex 是负数,则 startindex 被当作零。如果它比最大的字符位置索引还大,则它被当作最大的可能索引。

 

public class MainClass{

  public static void main(String[] arg){
    String str = "abcde";
   
    int index = 0;       
    index = str.indexOf('c'); 

    System.out.println(index);
  }

}

实例二

public class MainClass{

  public static void main(String[] arg){
    String str = "abcdea";
   
    int index = 0;       
    index = str.lastIndexOf('a');        

    System.out.println(index);
  }

}

搜索指定字符所在字符串中的位置

public class MainClass{

  public static void main(String[] arg){
    String str = "abcdea";
    int startIndex = 3;
   
    int index = 0;       
    index = str.indexOf('a', startIndex);        

    System.out.println(index);
  }

}

字符串最后面出来

public   int   lastIndexOf(String   str,   int   fromIndex)  
//从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。
//   k   <=   Math.min(fromIndex,   str.length())   &&   this.startsWith(str,   k)
//   就是在String中查找str出现的最后一次位置!如果位置 <=fromIndex就返回,否则返回-1
对你的情景就是:
//   banner中最后一次出现One的位置在9
//并且9 <10所以返回9

public class MainClass{

  public static void main(String[] arg){
    String str = "abcdeabcdef";
  
    int index = 0;       
    index = str.lastIndexOf("ab");        

    System.out.println(index);
  }

}

时间: 2024-07-30 00:34:56

java indexOf() lastIndexOf() 字符串查找函数的相关文章

java indexOf()简单字符查找实例

java indexof()简单字符查找实例 int indexof(string ch); 就是查找字符/字符串ch在index以后的位置,如果没有找到返回-1;index可以有可以没有,没有时默认为0. eg: string str="liuzheliuxing";        system.out.println((int)'i')                         // i的ascii        system.out.println(str.indexof(

sql 字符串查找函数

拼串的时候用char(39) 代替单引号,char(37)代替% set @sql = 'select * from authors where address like '+char(39)+char(37)+@str+char(37)+char(39) sql 字符串查找函数

java学习笔记之字符串查找函数

java中查找一个字符串是否包含一个字符,或者一段字符串java.lang.String indexOf方法  代码如下 复制代码 package com.java.lang;   public class StringX {    /**   * @param args   */  public static void main(String[] args) {   java.lang.String a = "my name is yuexiaosheng,my blog is java-er

C++ string 字符串查找函数

在写C++程序中,总会遇到要从一个字符串中查找一小段子字符串的情况,对于在C中,我们经常用到strstr()或者strchr()这两种方法.而对于C++的string,我们往往会用到find(). C++:#inlcude<string> C: #include<string.h> find():在一个字符串中查找一个指定的单个字符或字符数组.如果找到,就返回首次匹配的开始位置:如果没有查找到匹配的内容,就返回string::npos. find_first_of():在一个目标串

sql instr()与LOCATE()字符串查找函数

INSTR(str,substr) 返回字符串 str 中子字符串的第一个出现位置.这和LOCATE()的双参数形式相同,除非参数的顺序被颠倒.   代码如下 复制代码 mysql> SELECT INSTR('foobarbar', 'bar');         -> 4 mysql> SELECT INSTR('xbar', 'foobar');         -> 0 LOCATE(substr,str) , LOCATE(substr,str,pos) 第一个语法返回字

java indexOf() 和lastIndexOf()字符查找实现方法

java indexof() 和lastindexof()字符查找实现方法 class indexofdemo {   public static void main(string args[]) {     string s = "now is the time for all good men " + "to come to the aid of their country.";     system.out.println(s);     system.out

java字符串查找与替换

问题描述 importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.io.InputStreamReader;importjava.util.Random;importjava.util.Scanner;publicclassSt

C# 二进制字节流查找函数IndexOf

C# 二进制字节流查找函数IndexOf /// <summary> /// 报告指定的 System.Byte[] 在此实例中的第一个匹配项的索引. /// </summary> /// <param name="srcBytes">被执行查找的 System.Byte[].</param> /// <param name="searchBytes">要查找的 System.Byte[].</par

java 面试中的一道编写一个截取字符串的函数!!!!

函数|字符串 编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串. 但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个". package string;class SplitString { private String str; private int byteNum; publi