//String类
package test1;
import java.util.*;
public class Main {
public static void main(String[] args) {
String s = "abcabcbacdba";
System.out.println("字符串的长度为:"+s.length());
System.out.println("字符串中第一个字符:"+s.charAt(0));
System.out.println("字符c第一次出现的位置:"+s.indexOf('c'));
System.out.println("字符c最后一次出现的位置:"+s.lastIndexOf('c'));
System.out.println("子字符串第一次出现的位置:"+s.indexOf("ab"));
System.out.println("子字符串最后一次出现的位置:"+s.lastIndexOf("ab"));
System.out.println("*************");
String str="java";
System.out.println("将字符串转换成大写:"+str.toUpperCase());
//把字符串转化为字符串数组
char[] chars = str.toCharArray();
//遍历数组元组
for (int i=0;i<chars.length;i++){
if(i!=chars.length-1){
System.out.print(chars[i]+",");
}else {
System.out.print(chars[i]);
}
}
System.out.println("------------------");
String ss = " http : //localhost : 8080 ";
//字符串去除空格操作
System.out.println("去除字符串两端的空格"+ss.trim());
//字符串替换操作
System.out.println("去除字符串中所有空格后的结果:"+ss.replace(" ",""));
System.out.println("----------");
//字符串的判断操作
String s1="Starter";
String s2="St";
System.out.println("判断是否以字符串St开头:"+s1.startsWith("St"));
System.out.println("判断是否以字符串er结尾:"+s1.endsWith("er"));
System.out.println("判断是否包含字符串ar:"+s1.contains("ar"));
System.out.println("判断字符串是否为空:"+s1.isEmpty());
//判断两个对象的地址是否相同用==,判断两个对象的内容是否相同用equals
System.out.println("判断两个字符串是否相等:"+s1.equals(s2));
System.out.println("--------------------------");
//字符串的截取和分割
String st1="2018-01-24";
System.out.println("从第6个字符截取末尾的结果:"+st1.substring(5));
System.out.println("从第6个字符截取到第7个字符的结果:"+st1.substring(5,7));
//下面是字符串分割操作
System.out.println("分割后的字符串数组中的元素依次为:");
String[] strArray = st1.split("-");
for (int i=0;i<strArray.length;i++){
if(i!=strArray.length-1){
System.out.print(strArray[i]+",");
}else {
System.out.print(strArray[i]);
}
}
}
}
Output result:
the length of the string is: 12
first character in the string: a
position of the first occurrence of
character c : 2 position of the last occurrence of character c: 8 position of
the first occurrence of the substring: 0
substring Position of the last occurrence of the string: 3
Convert the string to uppercase: JAVA
j,a,v,a------------------
remove the spaces at both ends of the string http://localhost:8080
remove the string The result after all spaces: http://localhost:8080
Judge whether it starts with the string St: true
Judge whether it ends with the string er: true
Judge whether it contains the string ar: true
Judge whether the string is empty: false
Judge whether the two strings are equal: false
The result of truncating the end from the 6th character: 01-24 The result of truncating
from the 6th character to the 7th character:
The elements in the string array after the division of 01 are:
2018,01,24