JAVA中String类的方法(函数)总结--JAVA基础

1、concat()方法,当参数为两字符串时,可实现字符串的连接:

package cn.nxl123.www;

public class Test {
  public static void main(String[] args) {
    String string=new String("abcdef"),tString=new String("123");
    System.out.println("连接两个字符串:");
    System.out.println(string.concat(tString));//连接两个字符床串。
    System.out.println("也可以是:");
    System.out.println("abcdef".concat(tString));
    }
}

结果:

2、indexof()方法,当为indexof(String,int)这种形式时,此方法(函数)指定字符串从某个位置开始检索参数字符串,返回字符串首次出现的位置,如果没有检索到,返回-1,字符串是从0开始编号,"ABCDEFGHIJABC"就是0、1、2、3、4、5、6、7、8、9……

package cn.nxl123.www;

public class Test {
  public static void main(String[] args) {
    String s="ABCDEFGHIJABC";
    System.out.println(s.indexOf("B", 7));
    System.out.println(s.indexOf("B", 0));
    }
}

结果:

原文地址:https://www.cnblogs.com/qikeyishu/p/7553727.html