63 lines
2.7 KiB
Java
63 lines
2.7 KiB
Java
package com.inmind.string01;
|
||
/*
|
||
常用类_String的获取方法_length_concat_charAt_indexOf_subString
|
||
|
||
public int length () :返回此字符串的长度。
|
||
public String concat (String str) :将指定的字符串连接到该字符串的末尾。
|
||
public char charAt (int index) :返回指定索引处的 char值。
|
||
public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
|
||
public int indexOf(String str, int fromIndex)
|
||
|
||
public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
|
||
public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
|
||
*/
|
||
public class Demo04 {
|
||
|
||
public static void main(String[] args) {
|
||
//public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
|
||
String str = "helloworldjava";
|
||
String subStr1 = str.substring(5);
|
||
System.out.println(subStr1);
|
||
//public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex
|
||
//注意:java一定是包头不包尾
|
||
//获取world
|
||
System.out.println(str.substring(5, 10));
|
||
|
||
}
|
||
|
||
public static void method4(String[] args) {
|
||
//public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
|
||
String str = "123java321java 123";
|
||
//查询"java"出现的索引
|
||
int index = str.indexOf("java");
|
||
System.out.println(index);//3
|
||
//查询“sql”出现的索引
|
||
System.out.println(str.indexOf("sql"));//-1
|
||
//查询"java"第二次出现的索引
|
||
//public int indexOf(String str, int fromIndex)
|
||
System.out.println(str.indexOf("java", index + 1));//10
|
||
}
|
||
|
||
public static void method3(String[] args) {
|
||
//public char charAt (int index) :返回指定索引处的 char值。
|
||
String str = "helloworld";
|
||
char c = str.charAt(5);
|
||
System.out.println(c);//w
|
||
|
||
}
|
||
public static void method2(String[] args) {
|
||
//public String concat (String str) :将指定的字符串连接到该字符串的末尾。
|
||
String str = "hello";
|
||
String s = str.concat("world");
|
||
System.out.println(s);//helloworld
|
||
}
|
||
|
||
|
||
|
||
public static void method1(String[] args) {
|
||
//public int length () :返回此字符串的长度。常用于遍历,或者字符数据的长度
|
||
String str = "hello";
|
||
System.out.println(str.length());//5
|
||
}
|
||
}
|