zoukankan      html  css  js  c++  java
  • String字符串分隔

    public static void main(String[] args) throws IOException {
    String a = "a-b-c-d";
    //截取最后一个‘-’前面的所有内容
    String str1 = a.substring(0,a.lastIndexOf("-"));
    System.out.println("str1="+str1);

    //indexOf 返回第n个字符出现的索引,没有返回-1
    //截取第一个‘-’后面的内容
    String str2 = a.substring(a.indexOf("-")+1);
    System.out.println("str2="+str2);

    //截取第二个‘-’后面的内容
    String str3 = a.substring(a.indexOf("-",2)+1);
    System.out.println("str3="+str3);

    //返回第一个"-"前面的内容
    String str4 = a.substring(0,a.indexOf(str2)-1);
    System.out.println("str4="+str4);
    }

    indexOf() 方法有以下四种形式:

    • public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

    • public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

    • int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

    • int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。    

     @Test
    public static void main(String args[]) {
    String string = "aaa456ac";
      //查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.
    // indexOf(String str); 返回结果:-1,"b"不存在
      System.out.println(string.indexOf("b"));

      // 从第四个字符位置开始往后继续查找,包含当前位置
      System.out.println(string.indexOf("a", 3));//indexOf(String str, int fromIndex); 返回结果:6

      //(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99
      // 从头开始查找是否存在指定的字符 System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7
      System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7

      //从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。
      System.out.println(string.indexOf(97, 3));//indexOf(int ch, int fromIndex); 返回结果:6

      System.out.println(string.indexOf('a', 3));//indexOf(int ch, int fromIndex); 返回结果:6
      }
     

      输出结果为:

    -1
    6
    7
    7
    6
    6

    指定子字符串在字符串中第一次出现处的索引,从指定的索引开始。

    public static void main(String args[]) {
    String Str = new String("菜鸟教程:www.runoob.com");
    String SubStr1 = new String("runoob");
    String SubStr2 = new String("com");
    System.out.print("查找字符 o 第一次出现的位置 :");
    System.out.println(Str.indexOf('o'));
    System.out.print("从第14个位置查找字符 o 第一次出现的位置 :");
    System.out.println(Str.indexOf('o', 14));
    System.out.print("子字符串 SubStr1 第一次出现的位置:");
    System.out.println(Str.indexOf(SubStr1));
    System.out.print("从第十五个位置开始搜索子字符串 SubStr1 第一次出现的位置 :");
    System.out.println(Str.indexOf(SubStr1, 15));
    System.out.print("子字符串 SubStr2 第一次出现的位置 :");
    System.out.println(Str.indexOf(SubStr2));
    }
    以上程序执行结果为:
    查找字符 o 第一次出现的位置 :12
    从第14个位置查找字符 o 第一次出现的位置 :17
    子字符串 SubStr1 第一次出现的位置:9
    从第十五个位置开始搜索子字符串 SubStr1 第一次出现的位置 :-1
    子字符串 SubStr2 第一次出现的位置 :16
    linux下的docker操作命令及异常
  • 相关阅读:
    我的知识库(4) java获取页面编码(Z)
    知识库(3)JAVA 正则表达式 (超详细)
    The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts
    某人总结的《英语听力的技巧 》,挺搞的
    我的知识库(5)java单例模式详解
    构建可扩展程序
    SerialPort (RS232 Serial COM Port) in C# .NET
    Python学习笔记——String、Sequences
    UI题目我的答案
    jQuery学习系列学会操纵Form表单元素(1)
  • 原文地址:https://www.cnblogs.com/ketoli/p/14719364.html
Copyright © 2011-2022 走看看