day08-常用类_String的获取方法_length_concat_charAt_indexOf_subString
This commit is contained in:
73
day08/src/com/inmind/string01/Demo04.java
Normal file
73
day08/src/com/inmind/string01/Demo04.java
Normal file
@@ -0,0 +1,73 @@
|
||||
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 demo1(String[] args) {
|
||||
//public int length () :返回此字符串的长度。
|
||||
String str = "123456";
|
||||
System.out.println(str.length());//便于我们遍历字符串
|
||||
}
|
||||
|
||||
public static void demo2(String[] args) {
|
||||
//public String concat (String str) :将指定的字符串连接到该字符串的末尾。
|
||||
String str1 = "hello";
|
||||
String str2 = "world";
|
||||
String str3 = str1.concat(str2);
|
||||
System.out.println(str1);//hello
|
||||
System.out.println(str2);//world
|
||||
System.out.println(str3);//helloworld
|
||||
}
|
||||
|
||||
public static void demo3(String[] args) {
|
||||
//public char charAt (int index) :返回指定索引处的 char值。
|
||||
String str = "helloworld";
|
||||
//从str中获取w的字符
|
||||
char c = str.charAt(5);
|
||||
System.out.println(c);//w
|
||||
//请遍历字符串,将每个字符打印输出
|
||||
for (int i = 0; i < str.length(); i++) {
|
||||
System.out.println(str.charAt(i));
|
||||
}
|
||||
}
|
||||
|
||||
public static void demo4(String[] args) {
|
||||
//public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
|
||||
//public int indexOf(String str, int fromIndex)
|
||||
String str = "123java321java54321";
|
||||
//查找第一个j的索引
|
||||
int index = str.indexOf("j");//获取指定字符串的索引,如果找不到则返回-1
|
||||
System.out.println(index);//3
|
||||
//获取第一个java的索引
|
||||
index = str.indexOf("java");
|
||||
System.out.println(index);//3
|
||||
System.out.println("-----------------------");
|
||||
//获取第二个java的索引
|
||||
index = str.indexOf("java", 3+1 );
|
||||
System.out.println(index);//10
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
//public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
|
||||
//public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
|
||||
String str = "helloworld123";
|
||||
//请给我截取一个world123的字符串
|
||||
String substring = str.substring(5);
|
||||
System.out.println(substring);//world123
|
||||
//请给我截取一个world的字符串
|
||||
String substring1 = str.substring(5, 10);
|
||||
System.out.println(substring1);//world
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user