System.out.println(“i:”+i);
System.out.println(“b1:”+b1);
System.out.println(“o:”+o);
System.out.println(“a:”+a);
System.out.println(“b.toString():”+b.toString());
}
}
[](()(7)直接+
两个字符串相加
例如:
String s1 = “123”+“456”;
字符串和其他相加时会采用和println相同的策略
public class StringTest02 {
public static void main(String[] args) {
byte []b = {‘H’,‘e’,‘l’,‘l’,‘o’,‘3’};
Object o = null;
String s1 = “123”+new Integer(456);
String s2 = “123”+o;//o对象没有实例化返回null
String s3 = “123”+false;
String s4 = “123”+b;
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
}
}
任然来重点看一下字符数组,在+连接符下,会直接调用toString()从而返回其地址,不会访问其内容,编程与其他类型数组一样。
[](()2、字符串的操作
[](()(1)public String concat(String str)
进行拼接操作,若str是空字符串,则还返回原理的引用
public class StringTest02 {
public static void main(String[] args) {
String s1 = “123”;
String s2 = “456”;
System.out.println(“s1:”+s1);
System.out.println(“s1.hashCode():”+s1.hashCode());
s1 = s1.concat(s2);
System.out.println(“s1:”+s1);
System.out.println(“s1.hashCode():”+s1.hashCode());
}
}
s1.hashCode():1450575459
可以看出str不为空就会返回一个新的引用
[](()(2)其他相关方法
String replace(char oldChar,char newChar)
将所有的oldChar字符替换为newChar,
String toLowerCase()
、String toUpperCase()
转换大小写,
String trim()
去空白符,对于这些方法来说若不会改变原有字符串(如果需要改变需要对原有字符串重新赋值),
例如toLowerCase()
不含oldChar,转换为小写的不含小写字符串,或不含空白符等就不会修改原引用,
String substring(int beginIndex)
和String substring(int beginIndex,int endIndex)
是截取第beginIndex+1到结束的字符串和从第beginIndex+1到nedIndex的子字符串
package cn.itbluebox;
import javax.swing.*;
public class StringTest02 {
public static void main(String[] args) {
System.out.println(“replace↓”);
String s1 = “123123123”;
s1.replace(“1”,“a”);
System.out.println(s1);
s1 = s1.replace(“1”,“a”);
System.out.println(s1);
System.out.println(“toUpperCase↓”);
s1.toUpperCase();
System.out.println(s1);
s1 = s1.toUpperCase();
System.out.println(s1);
System.out.println(“toLowerCase↓”);
s1.toLowerCase();
System.out.println(s1);
s1 = s1.toLowerCase();
System.out.println(s1);
System.out.println(“trim↓”);
s1 = " “+s1+” “+s1+” ";
s1.trim();
System.out.println(s1);
s1= s1.trim();
System.out.println(s1);
System.out.println(“substring↓”);
s1.substring(5);
System.out.println(s1);
s1 = s1.substring(5);
System.out.println(s1);
s1.substring(2,4);
System.out.println(s1);
s1 = s1.substring(2,4);
System.out.println(s1);
}
}
[](()(3)其他相关方法
int indexOf(int ch)
,查询第一次出现的位置,若不含此字符串,
返回-1
,不要误以为写错了,确实是参数为int类型,这里指的是Unicode码,
int indexOf(int ch,int fromIndex)
功能相同但要从大于等于fromIndex的下标(注意和第几个分区)开始查询,
int indexOf(String str)
提前是str是子字符串,
返回第一个字符的下标,int index(String str,int fromIndex)
类似。
package cn.itbluebox;
public class StringTest02 {
public static void main(String[] args) {
String s = “abcabcdabcde”;
System.out.println(“s.indexOf(‘b’):”+s.indexOf(‘b’));
System.out.println(“s.indexOf(‘b’,1):”+s.indexOf(‘b’,1));//返回当前字符串当中b从下角标为1的位置(从第0号位开始)
System.out.println(“s.indexOf(‘b’,2):”+s.indexOf(‘b’,2));//返回当前字符串当中b从下角标为2的位置(从第0号位开始)
System.out.println(“s.indexOf(“abc”):”+s.indexOf(“abc”));//返回当前字符串当中abc从下角标为0的位置(从第0号位开始)
System.out.println(“s.indexOf(“abc”,1):”+s.indexOf(“abc”,1));//返回当前字符串当中abc从下角标为1的位置(从第0号位开始)
System.out.println(“s.indexOf(“ab”,-1):”+s.indexOf(“ab”,-1));//返回当前字符串当中ab从下角标为-1的位置(从第0号位开始)
System.out.println(“s.lastIndexOf(“ab”):”+s.lastIndexOf(“ab”));//s.length-1号下角标的相当于最后一个元素,往前第一次ab出现对应的下角标的值
System.out.println(“s.lastIndexOf(“c”,8):”+s.lastIndexOf(“c”,8));//8号下角标的相当于最后一个元素,往前第一次c出现对应的下角标的值
System.out.println(“s.lastIndexOf(“abc”,7):”+s.lastIndexOf(“abc”,7));//7号下角标的相当于最后一个元素,往前第一次abc出现对应的下角标的值
System.out.println(“s.lastIndexOf(“abc”,29):”+s.lastIndexOf(“abc”,29));//29号下角标的相当于最后一个元素,往前第一次abc出现对应的下角标的值
}
}
[](()(4)字符串的比较
int compareTo(String another);
(参数不能是null???),
返回值第一个不相同字符unicode码差值,若两一个字符串是另外一个字符串字串,
返回字符串长度差值,若两串相同则返回0,
此外还有int compareToIgnoreCase(String another);
比较方法与其类似,但是忽略大小写。
判断两个字符串相等不能用等号==
,要用boolean equals(Object anObj)
你看到的没错正是Object类型的参数,
但是你用其他对象做参数,参数的值为null的返回值返回false,
即使是StringBuffer对象也一样,null可以作为参数但是永远返回false。
boolean equalsIgnoreCase(String another)
忽略大小写。
具体案例
public class StringTest02 {
public static void main(String[] args) {
String a = “123”;
String b = “123”;
String c = “abc”;
String d = “ABC”;
System.out.println(a==b);//相同的字符串在字符串常量池当中是同一个引用/地址 == 对比的是引用/地址
System.out.println(a.equals(b));//equals对比的是两个字符串的内容是否相等
System.out.println(c.equals(d));
System.out.println(c.equalsIgnoreCase(d));//在忽略大小写的情况下对比其字符串的内容
}
}
[](()(5)将字符串转为基本类型parseXxx
例如转换为double
public class Str 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 ingTest02 {
public static void main(String[] args) {
String a = “123”;
double a_double = Double.parseDouble(a);//将字符串类型的数据转换为double 类型
System.out.println(“a_double:”+a_double);
}
}
Double.parseDouble()方法对应的源代码
public static double parseDouble(String s) throws NumberFormatException {
return FloatingDecimal.parseDouble(s);
}
此方法抛出NumberFormatException 数字转换异常,可以看出要想转换为double你的字符串必须要哦符合相应数据类型的格式。
但是有一个例外就是parseBoolean除非是字符串"true"或者"TRUE"(大小写不固定)
等返回true其余的情况都返回false。
也就是说可以不符合格式(这种情况例外)
对应案例代码
package cn.itbluebox;
public class StringTest02 {
public static void main(String[] args) {
String byteStr1 = “127”;
String byteStr2 = “128”;
System.out.println(Byte.parseByte(byteStr1));
try {
System.out.println(Byte.parseByte(byteStr2));
}catch (Exception e){
System.out.println(e);
}
String shortStr = “32767”;
String shortStr2 = “32768”;
System.out.println(Short.parseShort(shortStr));
try {
System.out.println(Short.parseShort(shortStr2));
}catch (Exception e){
System.out.println(e);
}
String intStr1 = “2147483647”;
String intStr2 = “2147483648”;
System.out.println(Integer.parseInt(intStr1));
try {
System.out.println(Integer.parseInt(intStr2));
}catch (Exception e){
System.out.println(e);
}
String floStr1 = “12”;
String floStr2 = “12f”;
String floStr3 = “12.1”;
String floStr4 = “12.121222”;
String floStr5 = “12.12122122121212122”;
System.out.println(Float.parseFloat(floStr1));
System.out.println(Float.parseFloat(floStr2));
System.out.println(Float.parseFloat(floStr3));
System.out.println(Float.parseFloat(floStr4));
System.out.println(Float.parseFloat(floStr5));
String douStr1 = “11”;
String douStr2 = “11d”;
String douStr3 = “11.0”;
System.out.println(Double.parseDouble(douStr1));
System.out.println(Double.parseDouble(douStr2));
System.out.println(Double.parseDouble(douStr3));
String longStr1 = “9223372036854775807”;
String longStr2 = “9223372036854775808”;
System.out.println(Long.parseLong(longStr1));
try {
System.out.println(Long.parseLong(longStr2));
}catch (Exception e){
System.out.println(e);
}
String booleanStr1 = “true”;
String booleanStr2 = “TRUE”;
String booleanStr3 = “TRue”;
String booleanStr4 = “FALSE”;
String booleanStr5 = “false”;
String booleanStr6 = “falSe”;
String booleanStr7 = “sassa”;
System.out.println(Boolean.parseBoolean(booleanStr1));
System.out.println(Boolean.parseBoolean(booleanStr2));
System.out.println(Boolean.parseBoolean(booleanStr3));
System.out.println(Boolean.parseBoolean(booleanStr4));
System.out.println(Boolean.parseBoolean(booleanStr5));
System.out.println(Boolean.parseBoolean(booleanStr6));
System.out.println(Boolean.parseBoolean(booleanStr7));
String charstr = “asas221wsaaw1wsasas”;
char[] chars = charstr.toCharArray();
for (char aChar : chars) {
System.out.print(“[”+aChar+“],”);
}
}
}
不符合格式或者超出数据要求范围则抛出异常
public class StringTest02 {
public static void main(String[] args) {
String s = “123456”;
String s1 = “123456”;
String s2 = new String(“123456”);
String s3 = “123”+“456”;
String s4 =“123”;
String s5 = s4 + “456”;
String s6 = “456”;
String s7 = s4 + s6;
String s8 = new String(s1);
String s9 = new String(s4+“456”);
System.out.print(“s1==s:”);
System.out.println(s1==s); //true
System.out.print(“s1==s2:”);
System.out.println(s1==s2); //false
System.out.print(“s1==s3:”);
System.out.println(s1==s3); //true
System.out.print(“s1==s5:”);
System.out.println(s1==s5); //false
System.out.print(“s1==s7:”);
System.out.println(s1==s7); //false
System.out.print(“s1==s8:”);
System.out.println(s1==s8); //false
System.out.print(“s1==s9:”);
System.out.println(s1==s9); //false
System.out.print(“s==s2.intern():”);
System.out.println(s==s2.intern()); //true
System.out.print(“s2==s2.intern():”);
System.out.println(s2==s2.intern());//false
System.out.print(“s2.intern()==s5.intern():”);
System.out.println(s2.intern()==s5.intern());//true
System.out.println(“------”);
System.out.println(s1.equals(s));
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s1.equals(s5));
System.out.println(s1.equals(s7));
System.out.println(s1.equals(s8));
System.out.println(s1.equals(s9));
}
}
==对比的是地址值(相同内容的字符串其地址值/引用是相同的)
equals比较的是器内容是否相同
尽管在输出中调用intern方法并没有什么效果,
但是实际上后台这个方法会做一系列的动作和操作。
在调用"ab".intern()
方法的时候会返回”ab”,但是这个方法会首先检查字符串池中是否有”ab”这个字符串,如果存在则返回这个字符串的引用,
否则就将这个字符串添加到字符串池中,然会返回这个字符串的引用。
System.out.println(s2==s2.intern());//false
s2是对象创建的时候存储在堆当中,
intern()是检测字符串是否在字符串常量池当中,
而s2不再字符串常量池所有会在字符串常量池当中创建"123456"的引用,
而堆当中的s2和字符串常量池当中是不一样的引用所以返回false
[](()4、字符串编码
public class StringTest02 {
public static void main(String[] args) {
String a =“121A212sasasa”;
String a1 = null;
try {
a1 = new String(a.getBytes(“ISO-8859-1”),“UTF-8”);
}catch (Exception e){
System.out.println(e);
}
System.out.println(a1);
}
}
public class ChangeCharset {
/** 7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块 */
public static final String US_ASCII = “US-ASCII”;
/** ISO 拉丁字母表 No.1,也叫作 ISO-LATIN-1 */
public static final String ISO_8859_1 = “ISO-8859-1”;
/** 8 位 UCS 转换格式 */
public static final String UTF_8 = “UTF-8”;
/** 16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序 */
public static final String UTF_16BE = “UTF-16BE”;
/** 16 位 UCS 转换格式,Little-endian(最高地址存放低位字节)字节顺序 */
public static final String UTF_16LE = “UTF-16LE”;
/** 16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识 */
public static final String UTF_16 = “UTF-16”;
/** 中文超大字符集 */
public static final String GBK = “GBK”;
/**
- 将字符编码转换成US-ASCII码
*/
public String toASCII(String str) throws UnsupportedEncodingException {
return this.changeCharset(str, US_ASCII);
}
/**
- 将字符编码转换成ISO-8859-1码
*/
public String toISO_8859_1(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, ISO_8859_1);
}
/**
- 将字符编码转换成UTF-8码
*/
public String toUTF_8(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, UTF_8);
}
/**
- 将字符编码转换成UTF-16BE码
*/
public String toUTF_16BE(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, UTF_16BE);
}
/**
- 将字符编码转换成UTF-16LE码
*/
public String toUTF_16LE(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, UTF_16LE);
}
/**
- 将字符编码转换成UTF-16码
*/
public String toUTF_16(String str) throws UnsupportedEncodingException{
return this.changeCharset(str, UTF_16);
}
/**
- 将字符编码转换成GBK码