day08-常用类-String-的获取方法_length_concat_charAt_indexOf_subString

This commit is contained in:
2026-05-16 13:46:44 +08:00
parent 9388b14f53
commit 7da8c2100f

View File

@@ -0,0 +1,62 @@
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
}
}