String
类代表字符串, Java中的所有字符串(例如"abc"
)都为此类的实例;
String
类包括用于检查序列的各个字符的方法,用于比较、搜索字符串,提取子字符串以及创建将所有字符转换为大写或小写的字符串的副本;- Java为字符串连接运算符(+)提供特殊支持,并为其他对象转换为字符串;字符串连接是通过
StringBuilder
(或StringBuffer
)类及其append
方法实现的;字符串转换是通过方法来实现toString
;
不变性
String源码如下:
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** The value is used for character storage. */
private final char value[];
}
从源码可以看出,String类被final修饰,保存数据的char数组也被final修饰,根据final关键字修饰的类不能被继承,修饰的方法不能被重写,修饰的变量不能二次赋值;故String是不可变类;因此String的大部分操作,都会返回新的String对象;
String str ="hello world !!";
// 这种写法是替换不掉的,必须接受 replace 方法返回的参数才行,
//这样才行:str =str.replace("l","dd");
str.replace("l","dd");
构造方法(常用)
初始化新创建的 |
通过使用平台的默认字符集解码指定的字节数组来构造新的 |
构造一个新的 |
通过使用平台的默认字符集解码指定的字节子阵列来构造新的 |
分配一个新的 |
分配一个新的 |
初始化新创建的 |
具体代码演示:
public static void main(String[] args) {
String s1=new String();
System.out.printf(s1);
System.out.println(s1.length());
byte [] by ={67,68,69,50};
String s2= new String (by);
System.out.println(s2);
System.out.println(s2.length());
String s3= new String (by,0,3);
System.out.println(s3);
System.out.println(s3.length());
char [] ch ={'1','2','3','我','爱','你'};
String s4 = new String (ch);
System.out.println(s4);
System.out.println(s4.length());
String s5= new String (ch,3,3);
System.out.println(s5);
System.out.println(s5.length());
String s6 = new String ("1234");
System.out.println(s6);
System.out.println(s6.length());
}
字符串乱码:由于不同环境文件编码格式不一样,导致字符串乱码,唯一的解决办法,就是在用到编码的地方,统一使用utf-8;并且使用构造器生成String对象时请指定编码格式,譬如String(byte[] bytes, Charset charset);
常用方法
char |
返回 |
int | compareTo(String anotherString) 按字典顺序比较两个字符串 |
String | concat(String str) 将指定的字符串连接到该字符串的末尾 |
boolean | contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时才返回true |
boolean | endsWith(String suffix) 测试此字符串是否以指定的后缀结尾。 |
boolean | equals(Object anObject) 将此字符串与指定对象进行比较。 |
boolean | equalsIgnoreCase(String anotherString) 将此 |
byte[] | getBytes(Charset charset) 使用给定的charset将该 |
int | indexOf(String str) 返回指定子字符串第一次出现的字符串内的索引。 |
int | indexOf(String str, int fromIndex) 返回指定子串的第一次出现的字符串中的索引,从指定的索引开始 |
boolean | isEmpty() 返回 |
int | lastIndexOf(String str) 返回指定子字符串最后一次出现的字符串中的索引。 |
int | length() 返回此字符串的长度。 |
boolean | matches(String regex) 告诉这个字符串是否匹配给定的 regular expression 。 |
String | replace(char oldChar, char newChar) 返回从替换所有出现的导致一个字符串 |
String[] | split(String regex) 将此字符串分割为给定的 regular expression的匹配。 |
boolean | startsWith(String prefix) 测试此字符串是否以指定的前缀开头。 |
String | substring(int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串。 |
String | substring(int beginIndex, int endIndex) 返回一个字符串,该字符串是此字符串的子字符串。 |
String | toLowerCase() 将所有在此字符 |
String | toUpperCase() 将所有在此字符 |
String | trim() 返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。 |
char[] | toCharArray() 将此字符串转换为新的字符数组。 |
常见应用场景
注意:在反射场景下,会用到类属性的首字母小写:
name.substring(0, 1).toLowerCase() + name.substring(1)
相等判断:即从底层结构出发,String的底层结构是char数组,所以只需挨个比较char数组中字符是否相等即可;
//sting equals方法源代码
/** The value is used for character storage. */
private final char value[];
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
若需要判断两者是否相等,可以按照String的equals方法的思路,即从底层结构出发,String的底层结构是char数组,所以只需挨个比较char数组中字符是否相等即可;
常用方法代码演示:
public class TestString {
public static void main(String[] args) {
String str1 = new String("abcdef");//初始化一个String对象
System.out.println(str1.length());//输出字符串的长度
System.out.println(str1.indexOf("c"));//输出指定字符在字符串中的位置
System.out.println(str1.charAt(3));//输出指定位置的值
String str2 = new String("wangmeixi");
System.out.println(str1.equals(str2));//比较两个字符串是否是相等的。
String str3 = new String("abcdefghijk");
System.out.println(str3);//输出str3的字符串
System.out.println( str3.replace("def","fed"));//输出替换之后的字符串
}
}
public class TestString2 {
public static void main(String[] args) {
//split()
String[] str1;//声明一个空的数组
String s = "The time is going quickly!";//声明一个字符串
str1=s.split(" ");//根据给定正则表达式的匹配拆分字符串。
for (int i=0;i<str1.length;i++){
System.out.println(str1[i]);
}
str1 = s.split(" ",3);//根据匹配给定的正则表达式来拆分字符串。
for (int i=0;i<str1.length;i++){
System.out.println(str1[i]);
}
//substring()
System.out.println(s.substring(4,7));//预计输出tim
//trim()
String ss = " dssd fkdj ";
System.out.println(ss.trim());//输出字符串前面的空白处
//format
//将其他类型的数据和字符串按照指定的格式输出
int d = 2018;
String sd = "Study Hard!";
System.out.println(String.format("%d年加油,%s",d,sd));
//toLowerCase 、toUpperCase
System.out.println(sd.toLowerCase());//将所有大写字母变为小写
System.out.println(sd.toUpperCase());//将所有小写字母变为大写
}
}
String和int之间的相互转换:
- String 转换 int 类型:通过int对应的包装类的方法来实现
//first
int a1=Integer.parseInt("123");
//second
int a2=Integer.valueOf("123");
- int 转换成字串 String
//first
String s1=String.valueOf(1);
//second
String s2=Integer.toString(1);
//three
String s3=""+1;
最后,补充String.format()的使用:
https://blog.csdn.net/lonely_fireworks/article/details/7962171