day08-常用类-String-的获取方法_length_concat_charAt_indexOf_subString
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package com.inmind.string01;
|
||||
/*
|
||||
4.常用类-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 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 = "helloworld";
|
||||
//获取world字符串,java中是包头不包尾
|
||||
String substring = str.substring(5);
|
||||
System.out.println(substring);
|
||||
System.out.println(str);//subString方法不会破坏原本的字符串
|
||||
//我截图low单词
|
||||
//public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
|
||||
String substring1 = str.substring(3, 6);//java中是包头不包尾
|
||||
System.out.println(substring1);
|
||||
}
|
||||
|
||||
|
||||
public static void indexOfMethod(String[] args) {
|
||||
//public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
|
||||
String str = "123java321java54321";
|
||||
//获取第一个java的索引,获取第一个java的第一个字符的索引
|
||||
int index = str.indexOf("java");
|
||||
System.out.println(index);//3
|
||||
System.out.println("--------------------");
|
||||
//获取第二个java的索引
|
||||
int index1 = str.indexOf("java", index + 1);
|
||||
System.out.println(index1);//10
|
||||
}
|
||||
|
||||
public static void charAtMethod(String[] args) {
|
||||
//public char charAt (int index) :返回指定索引处的 char值。
|
||||
String s1 = "hello world";
|
||||
//获取空字符
|
||||
char c = s1.charAt(5);
|
||||
System.out.println("空字符:"+c);
|
||||
//获取第二个o字符
|
||||
System.out.println(s1.charAt(7));
|
||||
System.out.println("------------------------------");
|
||||
//字符串的遍历
|
||||
/*int length = s1.length();
|
||||
for (int i = 0; i < length; i++) {
|
||||
System.out.println(s1.charAt(i));
|
||||
}*/
|
||||
for (int i = 0; i < s1.length(); i++) {
|
||||
System.out.println(s1.charAt(i));
|
||||
}
|
||||
}
|
||||
|
||||
public static void concatMethod(String[] args) {
|
||||
//public String concat (String str) :将指定的字符串连接到该字符串的末尾。
|
||||
String s1 = "hello";
|
||||
String s2 = "world";
|
||||
String s3 = s1+s2;
|
||||
String s4 = s1.concat(s2).concat("java");//链式调用
|
||||
System.out.println(s3);
|
||||
System.out.println(s4);
|
||||
}
|
||||
|
||||
|
||||
public static void lengthMethod(String[] args) {
|
||||
//public int length () :返回此字符串的长度。
|
||||
String s1 = "123456";
|
||||
System.out.println(s1.length());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user