千教万教教人求真,千学万学学做真人。——陶行知
字符串:字符的序列,它是组织字符的基本数据结构
可以分为两类:
- 创建之后不会再做修改和变动的字符串常量String类
- 创建之后允许再做更改和变化的字符串变量StringBuffer类和StringBuilder类
一、String
字符串的基本操作:
实践代码,搓搓小手动起来(=!=):
String s = “hello”;
String str = new String(“hello”);
- 字符串长度,字符个数(length(),返回值int类型)
System.out.println(s.length());//5
- 字符串比较(compareTo(),返回值int类型)
2个字符串的比较有点复杂 - 判断两个字符串内容是否相同(equals(),返回值boolean类型)
不得不提是 == 和equals()的不同:
8大基本数据类型用 == 判断值是否相等
引用数据类型中:
String s = “aaaa”;
是把"aaaa"赋值给字符串变量s,这和对象无关
s=="aaaa"是true,s.equals(“aaaa”)也是true
String str = new String(“aaaa”);e
此时new String(“aaaa”)创建了一个类String实例化的对象
这个对象在内存中有自己的存储地址
变量str指向了这个对象,被绑定在一起,str存储的是这个存储地址
比喻:"aaaa"是str的衣服,存储地址才是str的肉体
str==s是false,str.equals(s)是true
System.out.println(s.equals("hello"));//true
System.out.println(s.equals("hello"));
System.out.println(s=="hello");//true
System.out.println(s==str);//false
System.out.println(str.equals("hello"));//true
System.out.println(str.equals(s));//true
System.out.println(str=="hello");//false
System.out.println(str==s);//false
- 字符串的检索,charAt(索引),返回值char类型
System.out.println(s.charAt(0));//h
//遍历,length()字符串长度
for (int i=0;i<s.length();i++) {
System.out.println(s.charAt(i)); //h e l l o
}
- 字符串中元素的索引indexOf(),返回值int类型
返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1
System.out.println(s.indexOf("h"));//0
System.out.println(s.indexOf("e"));//1
System.out.println(s.indexOf("hello"));//0
System.out.println(s.indexOf("w"));//-1
System.out.println(s.indexOf(1));//-1
- 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1
lastIndexOf()返回值int类型
System.out.println(s.lastIndexOf("h"));//0
System.out.println(s.lastIndexOf("e"));//1
System.out.println(s.lastIndexOf("l"));//3
System.out.println(s.lastIndexOf("o"));//4
- substring(索引)或substring(索引a,索引b)
返回字符串的子字符串(索引的元素–最后的元素)或(索引a的元素–[索引b-1]的元素)
System.out.println(s.substring(0));//"hello"
System.out.println(s.substring(1));//"ello"
System.out.println(s.substring(2));//"llo"
System.out.println(s.substring(0,4));//"hell"
System.out.println(s.substring(1,3));//"el"
- 字符串的修改:注意,String的修改都不会修改原字符串,修改后的字符串以新字符串的形式返回
- toLowerCase() 方法将字符串转换为小写
System.out.println(s.toLowerCase());//hello
- replace(原字符,新字符) 替换
System.out.println(s.replace(s,"happy"));//happy
- trim() 方法用于删除字符串的头尾空白符
System.out.println(" hello ".trim());//hello
- concat() 方法用于将指定的字符串参数连接到字符串上
System.out.println(s.concat(" Mr Jiang"));//hello Mr Jiang
- 字符串类与其他类型的转换
变为字符串类型:
valueOf(char data[])
valueOf(char data[],int offset,int count)
valueOf(boolean b)
valueOf(char c)
valueOf(int i)
valueOf(long l)
valueOf(float f)
valueOf(double d)
- 字符串类型【数字】变成其他类型:
Byte.parseByte(String s)
Short.parseShort(String s)
Integer.parseInt(String s)
Long.parseLong(String s)
Float.parseFloat(String s)
Double.parseDouble(String s)
二、StringBuffer和StringBuilder
StringBuffer和StringBuilder类的对象能够被多次的修改,并且不产生新的未使用对象
两者的比较:
-
速度:StringBuilder > StringBuffer
-
线程安全:StringBuffer > StringBuilder(的方法不能同步访问,线程不安全)
要求线程安全必须要用StringBuffer
StringBuffer的append方法能有效地将给定的数据转换成字符串,然后将该字符串的字符追加或插入到字符串缓冲区中,就像“+”,把2个字符串粘起来。
实践代码,搓搓小手动起来(=!=):
Double a = 100.87878;
StringBuffer sss = new StringBuffer("sss");
sss.append(a);
System.out.println(sss);//sss100.87878
今天学习分享的内容到此为止了,本是想要激励自己学下去,但发现快乐更是知识在于分享!
作为初学者对于Java的理解实在浅薄,如有错误请指正,万分感谢!