1.String字符串的构造
string字符串类型
双击Shift,选择classes,搜素String
字符串是不可变对象,被final修饰

区别于C语言,JAVA当中字符串没有以\0结尾的说法
String类常用的三种构造方法
public class Test {
public static void main(String[] args) {
//1)
String s1="hello";
//2)
String s2=new String("world");
//3)
char []array={'h','a','h','a'};
String s3=new String(array);
System.out.println(s3);
}
}
" "双引号括起来的也是String类型对象,string是引用类型
2.String的比较
== 对于引用类型比较的是引用中的地址,内置类型比较的是变量中的值
public class Test {
public static void main(String[] args) {
//str1和str2不一样,两个是新new的对象,指向不一样,
//所存储的地址不一样
String str1 =new String("abcde");
String str2 =new String("abcde");
System.out.println(str1==str2);
//判断存储的内容是否一样
System.out.println(str1.equals(str2));
System.out.println("================");
//字符串常量池 (哈希表)?
//如果常量池已经有的字符串就不会再存取
String str3="abc";
String str4="abc";
System.out.println(str3==str4);
String str5=str4;
//str5指向str4所指向的对象,所以一样
System.out.println(str4==str5);
}

2) 

前k个字符相等(k为两个长度最小值,相等的长度范围),返回长度差值

equals返回的是boolean类型,compareTo返回的是int类型,,compareToIgnoreCase与CompareTo比较方式相同,只是忽略字符大小写
3.字符串的查找
常用的方法
public static void main(String[] args) {
String s="hello world";
//1)返回index位置上字符,不能越界 char charAt(int index)
System.out.println(s.charAt(2));//l
//2)返回ch第一次出现的位置,没有返回-1 int indexOf(int ch)
//int indexOf(int ch,int fromIndex)从fromIndex位置开始找ch第一次出现的位置
//int index(String str)返回str第一次出现位置
//int indexOf(String str,int fromIndex)
System.out.println(s.indexOf('e'));//1
//3)int lastIndexOf(int ch)从后往前找返回ch第一次出现的位置
//int lastIndexOf(int ch,int fromIndex)从后往前
//int lastIndex(String str)
//int lastIndex(String str,int fromIndex)
System.out.println(s.lastIndexOf("o"));//7
//空格会计入字符串的总长度,并且在字符串中都有其对应的下标。
System.out.println(s.indexOf("o w"));//4
}

4.转化
数值和字符串转化
public static void main(String[] args) {
//数字转化字符串
String s1=String.valueOf(1234);
String s2=String.valueOf(12.34);
String s3=String.valueOf(true);
//返回路径(对象)
String s4=String.valueOf(new Student("小明",8) );
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println("===============");
//字符串转为数字
int data1=Integer.parseInt("1314");
double data2=Double.parseDouble("13.14");
System.out.println(data1);
System.out.println(data2);
}

大小写转化也不是在原来的字符串上改变,是产生一个新字符串,只转换字母
public static void main(String[] args) {
//大小写转化
String s1="HELLO";
String s2="hello";
//小写
System.out.println(s1.toLowerCase());
//大写
System.out.println(s2.toUpperCase());
//字符串转为数组 toCharArray
String s3="world";
char[] ch=s3.toCharArray();
for (int i = 0; i < ch.length; i++) {
System.out.println(ch[i]);
//数组转为字符串构造方法
}
//格式化 format
String s=String.format("$%d-%c-%d",2014,'e',6);
System.out.println(s);
}

String trim()方法去掉字符串的左右空格
5.字符串替换,拆分,截取
替换
字符串是不可变对象,替换不是修改当前字符串,而是产生一个新的字符串
replace
public static void main(String[] args) {
String str="aaccbgdioaacien";
//替换所有内容
System.out.println(str.replaceAll("aa","zz"));
// //替换首个内容
String s=str.replaceFirst("c","!");
System.out.println(s);
}

拆分

public static void main(String[] args) {
String s="All roads lead to Rome";
String[] result=s.split(" ");
// for(String str:result){
// System.out.println(str);
// }
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
String[] result2=s.split("",3);
for(String str:result2){
System.out.println(str);
}
}
按字符".","+","*"..等拆分要加上转义字符"\\"

截取
substring
public static void main(String[] args) {
//String subString (int beginIndex)从什么为直截取
// String subString (int beginIndex,int endIndex)左闭右开的范围截取
String s="helloworld";
System.out.println(s.substring(5));
System.out.println(s.substring(3));
System.out.println(s.substring(0,4));
}
tj
6. 借助StringBuilder和StringBuffer修改字符串
package demo7;
//StringBuilder和StringBuffer不能直接赋值,要通过new
public class Test {
public static void main(String[] args) {
//String变为StringBuilder
StringBuilder stringbuilder=new StringBuilder("abc");
System.out.println(stringbuilder);
//在append过程中只有一个对象,不产生新对象,添加到它本身
//拼接产生的是新对象
stringbuilder.append(1);
stringbuilder.append("cde");
System.out.println(stringbuilder);
//锁 synchronized () 多线程情况下stringbuffer 单线程 stringbuilder
//将index位置的字符设置成ch
stringbuilder.setCharAt(1,'z');
System.out.println(stringbuilder);
//插入
stringbuilder.insert(2,"!!");
System.out.println(stringbuilder);
//StringBuffer和StringBuilder大部分功能相似,内容可以修改
//StringBuilder变为String调用toString方法
}
}

484



被折叠的 条评论
为什么被折叠?



