/**
* String类的特点:
* 字符串对象一旦被初始化就不会被改变
*/
String s1 = "abc";//在常量池中创建字符串对象
String s2 = new String("abc");//创建两个字符串对象,一个new,一个在堆内存中
System.out.println(s1==s2);//比较地址:false
System.out.println(s1.equals(s2));//比较内容:true
//String类中的equals复写Object中的equals建立了String类自己的判断字符串对象是否相同的依据。
//其实就是比较字符串内容
字符串的常见方法
public class packDemo{
public static void main(String[] args){
String s1 = "abcdefg";//在常量池中创建字符串对象
System.out.println(s1.length());//7
System.out.println(s1.charAt(2));//c
System.out.println(s1.indexOf('a'));//0
System.out.println(s1.lastIndexOf('f'));//5
System.out.println(s1.lastIndexOf('e', 1));
System.out.println(s1.substring(1,5));//bcde,包含begin,不包含end
/**
* 转换
* 1.将字符串变成字符串数组(字符串切割)
* String[] (String regex);//设计到正则
* 2.将字符串变成字符数组
* char[] toCharArray();
* 3.将字符串转成字节数组
* byte[] getBytes();
* 4.将字符串中的字母转换成大小写。
* String toUpperCase();大写
* String toLowerCase();小写
* 5.对字符串中的内容进行替换
* String replace(char oldch,char newch);
* 6.将字符串两端空格去除,返回新的字符串
* trim();
* 7.将字符串进行连接
* String concat(String str);
* 判断
* 1.两个字符串内容是否相同
* boolean equals(Object obj);
* 2.字符串中是否包含某一个字符串
* boolean contains("bc");
* 3.字符串是否以指定字符串开头或结尾
* boolean startsWith(String str);
* boolean endsWith(String str);
* 比较
* 1.按字典顺序比较两个字符串
* int compareTo(String anotherstr);
*/
String s2 = new String("a,b,c");//创建两个字符串对象,一个new,一个在堆内存中
String[] arr = s2.split(",");//["a","b","c"]
char[] arr1 = s2.toCharArray();//['a',',','b',',','c']
byte[] arr2 = s2.getBytes();
System.out.println(" abc ".trim());//abc
System.out.println("abc".concat("bdf"));//abcbdf
System.out.println(String.valueOf(4)+1);//41
String s = "abc";
System.out.println(s.equals("ABC".toLowerCase()));//true
System.out.println(s.equalsIgnoreCase("ABC"));//true 忽略大小写比较字符串内容是否相同
System.out.println(s.contains("ab"));//true
System.out.println(s.startsWith("a"));//true
System.out.println(s.endsWith("b"));//false
/**
* intern()方法
*/
String a1 = new String("abc");
String a2 = a1.intern();
//intern方法可以对字符串池进行操作,返回字符串对象规范化表现形式
System.out.println(a1==a2);//false
System.out.println("zb".compareTo("ds"));
// for(int i=0;i<arr2.length;i++){
// System.out.println(arr2[i]);
// }
//System.out.println(s1==s2);//比较地址:false
//System.out.println(s1.equals(s2));//比较内容:true
//String类中的equals复写Object中的equals建立了String类自己的判断字符串对象是否相同的依据。
//其实就是比较字符串内容。
// stringConstructorDemo();
// stringConstructorDemo2();
}
/**字符串初始化 */
public static void stringConstructorDemo(){
byte[] arr = {65,66,67,68};
String s1 = new String(arr);
System.out.println("s1 = "+s1);//s1 = ABCD
}
public static void stringConstructorDemo2(){
char[] arr = {'w','a','l','l','t'};
String s1 = new String(arr,1,3);//all
System.out.println("s1 = "+s1);//s1 = wallt
}
}
字符串数组排序
public class packDemo{
public static void main(String[] args){
String[] arr = {"abc","df","jh","kpp","zxy","cdf","as"};
//排序字符串
sortArr(arr);
show(arr);
}
public static void sortArr(String[] arr){
for(int i=0;i<arr.length;i++){
for(int j=i+1;j<arr.length;j++){
if(arr[i].compareTo(arr[j])>0){
swap(arr,i,j);
}
}
}
}
public static void show(String[] arr){
for(int i=0;i<arr.length;i++){
if(i!=arr.length-1){
System.out.println(arr[i]);
}
else
System.out.println(arr[i]+" ");
}
}
public static void swap(String[] arr,int i,int j){
String temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
字符串中包含多少个指定子串
public static int getKeyStringCount(String str,String key){
int count = 0;
int index = 0;
while(str.indexOf(key,index)!=-1){
index = str.indexOf(key,index)+key.length();
count++;
}
return count;
}
最长相同子串
public static String getMaxSubString(String s1,String s2){
for(int i=0;i<s2.length();i++){
for(int a=0,b=s2.length()-i;b!=s2.length()+1;a++,b++){
String sub = s2.substring(a,b);
if(s1.contains(sub))
return sub;
}
}
return null;
}
去除字符串两端空白
public static String myTrim(String str){
int start = 0,end = str.length()-1;
while(str.charAt(start)==' '&&start<=end)
start++;
while(str.charAt(end)==' '&&start<=end)
end--;
return str.substring(start,end+1);
}