Java的字符串用String表示

1.String特点:

  • 可以直接使用"..."表示一个字符串,不强制使用new String
  • 内容不可变
String s1 = "hello world";
        String s2 = new String("Hello");
        String s1 = "hello world";
        String s2 = new String("Hello");

2.判断是否相等equals

String s = "hello world";
        s.equals("hello world");
        s.equalsIgnoreCase("Hello WORLD");//忽略大小写
        String s = "hello world";
        s.equals("hello world");
        s.equalsIgnoreCase("Hello WORLD");//忽略大小写

3.是否包含一个子串

  • contains:是否包含一个字串,boolean类型
  • indexOf:返回子串在原字符中的第一个位置的索引,没有返回-1,int类型
  • lastIndexOf:返回子串在原字符中最后一个位置的索引,没有返回-1,int类型
  • startsWith:原字符串是否以子串开始,boolean类型
  • endsWith:原字符串是否以子串结尾,boolean类型
String s = "hello world";
        System.out.println(s + "是否包含world:" + s.contains("world"));
        System.out.println(s + "中第一个o的位置:"+ s.indexOf("o"));
        System.out.println(s + "最后一个o的位置:" + s.lastIndexOf("o"));
        System.out.println(s+ "是以ll开头:" + s.startsWith("ll"));
        System.out.println(s+"是以ld结尾:" + s.endsWith("ol"));
        String s = "hello world";
        System.out.println(s + "是否包含world:" + s.contains("world"));
        System.out.println(s + "中第一个o的位置:"+ s.indexOf("o"));
        System.out.println(s + "最后一个o的位置:" + s.lastIndexOf("o"));
        System.out.println(s+ "是以ll开头:" + s.startsWith("ll"));
        System.out.println(s+"是以ld结尾:" + s.endsWith("ol"));

java语句在结束后逗号的作用 java字符串结束标志_java语句在结束后逗号的作用

4.trim()移除字符串的空格

  • 移除首尾空白字符,包含空格 \t \r \n
  • 注意:trim不改变字符串内容,而是返回新字符串
String s = " \thello world\r\n";
        System.out.println(s.trim());
        System.out.println("a"+s+"a");//首尾添加字符可以看到原字符包含的空格
        String s = " \thello world\r\n";
        System.out.println(s.trim());
        System.out.println("a"+s+"a");//首尾添加字符可以看到原字符包含的空格

java语句在结束后逗号的作用 java字符串结束标志_java语句在结束后逗号的作用_02

5.提取字串

String s = "hello world";
        System.out.println("4:"+s.substring(4));
        System.out.println("4-8:"+s.substring(4,8));
        String s = "hello world";
        System.out.println("4:"+s.substring(4));
        System.out.println("4-8:"+s.substring(4,8));

java语句在结束后逗号的作用 java字符串结束标志_System_03

6.大小写转换

  • toUpperCase()转换为大写,可以加地区
  • toLowerCase()转换为小写,可以加地区
String s = "hello world";
        System.out.println(s.toUpperCase());
        System.out.println(s.toLowerCase());
        System.out.println(s.toLowerCase(Locale.CANADA));
        System.out.println(s.toUpperCase(Locale.CANADA));
        String s = "hello world";
        System.out.println(s.toUpperCase());
        System.out.println(s.toLowerCase());
        System.out.println(s.toLowerCase(Locale.CANADA));
        System.out.println(s.toUpperCase(Locale.CANADA));

java语句在结束后逗号的作用 java字符串结束标志_System_04


关于java.util.locale见易佰教程 顺便吐槽以下,台独无处不在

java语句在结束后逗号的作用 java字符串结束标志_java语句在结束后逗号的作用_05

7.替换字串replace

replace有2个重载方法,分别替换1个字符和1串字符
正则表达式替换字串

  • replaceAll(String regex, String replacement)
  • replaceFirst(String regex, String replacement)
String s = "hello world";
        System.out.println(s.replace("l","r"));
        System.out.println(s.replace("ll","er"));
        String s = "hello world";
        System.out.println(s.replace("l","r"));
        System.out.println(s.replace("ll","er"));

java语句在结束后逗号的作用 java字符串结束标志_java语句在结束后逗号的作用_06

8.分割字符串split

通过正则表达式分割字符串

String s = "hello world";
        String[] ss = s.split("[e|o]");
        System.out.println(Arrays.toString(ss));
        String s = "hello world";
        String[] ss = s.split("[e|o]");
        System.out.println(Arrays.toString(ss));

java语句在结束后逗号的作用 java字符串结束标志_ico_07

9.拼接字符串join()

使用String的静态方法join拼接字符串

String[] arr = {"A","B","C"};
        String s = String.join("~~",arr);
        System.out.println(s);
        String[] arr = {"A","B","C"};
        String s = String.join("~~",arr);
        System.out.println(s);

java语句在结束后逗号的作用 java字符串结束标志_字符串_08

10.String类和其他数据类型的转换

如果要任意数据转换为String,可以使用String的static方法:

//String.valueOf(可以传入int类型、布尔类型、object对象)
        System.out.println(String.valueOf(123));
        System.out.println(String.valueOf(true));
        System.out.println(String.valueOf(new Object()));
        System.out.println(new Object().toString());        
        //String.valueOf(可以传入int类型、布尔类型、object对象)
        System.out.println(String.valueOf(123));
        System.out.println(String.valueOf(true));
        System.out.println(String.valueOf(new Object()));
        System.out.println(new Object().toString());

java语句在结束后逗号的作用 java字符串结束标志_java语句在结束后逗号的作用_09


把String转换为其他类型

System.out.println(Integer.parseInt("123"));
        System.out.println(Integer.valueOf("123"));
        System.out.println(Integer.getInteger("123"));//去环境变量查找有无123这个环境变量
        System.out.println(Integer.parseInt("123"));
        System.out.println(Integer.valueOf("123"));
        System.out.println(Integer.getInteger("123"));//去环境变量查找有无123这个环境变量

java语句在结束后逗号的作用 java字符串结束标志_ico_10

11.String和char的互相转换

String类型内部为一个char类型的数组,因此可以把String和char类型互相转换

  • String转换为char数组:toCharArray()
  • char数组转换为String:new String(char[])
String s = "hello world";
        char[] cs = s.toCharArray();
        String s2 = new String(cs);
        System.out.println(Arrays.toString(cs));
        System.out.println(s2);
        String s = "hello world";
        char[] cs = s.toCharArray();
        String s2 = new String(cs);
        System.out.println(Arrays.toString(cs));
        System.out.println(s2);

java语句在结束后逗号的作用 java字符串结束标志_ico_11

12.String和byte的转换

String转换为byte[]:

  • getBytes() 使用系统自带的编码格式不推荐。因为系统默认编码不一致。windows使用gbk编码,Linux、Mac使用utf-8编码
  • getBytes(String) 指定编码格式
  • getBytes(Charset) 指定编码格式
public static void main(String[] args) throws UnsupportedEncodingException {
        //字符串转化为byte[]
        byte[] bs1 = "hello world".getBytes();
        byte[] bs2 = "女人找不到,男人也能凑合".getBytes("UTF-8");
        byte[] bs3 = "智能ABC".getBytes(StandardCharsets.UTF_8);

        System.out.println(Arrays.toString(bs1));
        System.out.println(Arrays.toString(bs2));
        System.out.println(Arrays.toString(bs3));
        //byte[] 转换为String:
        String s1 = new String(bs1);
        String s2 = new String(bs2);
        String s3 = new String(bs3);

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        //字符串转化为byte[]
        byte[] bs1 = "hello world".getBytes();
        byte[] bs2 = "女人找不到,男人也能凑合".getBytes("UTF-8");
        byte[] bs3 = "智能ABC".getBytes(StandardCharsets.UTF_8);

        System.out.println(Arrays.toString(bs1));
        System.out.println(Arrays.toString(bs2));
        System.out.println(Arrays.toString(bs3));
        //byte[] 转换为String:
        String s1 = new String(bs1);
        String s2 = new String(bs2);
        String s3 = new String(bs3);

        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
    }

java语句在结束后逗号的作用 java字符串结束标志_System_12

计算机中只能表示数字,如果要表示字符,需要把字符编码为数字。
最早的编码是ASCII码,一个字符占一个字节,ASCII码最高位是0,因此最多表示128个字符(2^7)。如字符A的编码是0x41。
对于中文来说,显然是不够用的,因此一个中文字符需要占用2个字节。中文编码规范有GB2132,GBK,GB18030。中文编码的第一个字节最高位是1。如GBK编码中,“中”两个字节是d6和d0。
同理其他不同的国家使用双字节进行编码,就会冲突。比如页面有多国文字,如果编码冲突,就会显示乱码。为了解决这一问题,出现了全球统一编码:Unicode。
unicode确保全球所有文字都有唯一编码,这样就不会再有冲突。一个Unicode字符通常占2个字节,Java使用Unicode编码。
问题:有了Unicode,为什么还需要UTF-8?
原因:1.英文Unicode编码和ASCII不一致。一个英文字符,Unicode占用2个字节,最高字节是0;ASCII占用1个字节。因此包含大量英文的文本会浪费一定的空间。
2.其次原来的软件不能识别Unicode编码会报错
UTF-8编码是变长编码

  • 英文UTF-8和ASCII一致
  • 其他Unicode字符需2-6字节不等 “中”:GBK~d6d0 ,Unicode:4e2d,UTF-8~e4b8ad
/*
        "A"在ASCII、Unicode、UTF-8不同编码中的值
        utf-8和ASCII都返回一个10进制数65。
        将其转换为16进制,结果为41。
        Unicode为2个字节,补全第一个字节,使用Java自身的Unicode打印编码为0041的char,结果为A
        */
        String a = "A";
        byte[] bs1 = a.getBytes("UTF-8");
        System.out.println(Arrays.toString(bs1));
        byte[] bs2 = a.getBytes(StandardCharsets.US_ASCII);
        System.out.println(Arrays.toString(bs2));
        System.out.println(Integer.toHexString(65));
        System.out.println('\u0041');

        /*
        "A"在ASCII、Unicode、UTF-8不同编码中的值
        utf-8和ASCII都返回一个10进制数65。
        将其转换为16进制,结果为41。
        Unicode为2个字节,补全第一个字节,使用Java自身的Unicode打印编码为0041的char,结果为A
        */
        String a = "A";
        byte[] bs1 = a.getBytes("UTF-8");
        System.out.println(Arrays.toString(bs1));
        byte[] bs2 = a.getBytes(StandardCharsets.US_ASCII);
        System.out.println(Arrays.toString(bs2));
        System.out.println(Integer.toHexString(65));
        System.out.println('\u0041');

java语句在结束后逗号的作用 java字符串结束标志_java_13

public static void main(String[] args) throws UnsupportedEncodingException {
        //打印‘中’在utf-8,unicode,gbk中的不同编码值
        String a = "中";
        System.out.print("\nutf-8编码转换为16进制后:");
        byte[] bs1 = a.getBytes("UTF-8");
        for(byte b:bs1){
            System.out.print((String)Integer.toHexString(b)+"\t");
        }
        System.out.print("\nGBK编码转换为16进制后:");
        byte[] bs2 = a.getBytes("GBK");
        for(byte b:bs2){
            System.out.print(Integer.toHexString(b)+"\t");
        }
        char c = '中';
        int n = c;
        System.out.print("\nUnicode编码转换为16进制后:"+Integer.toHexString(n));
        System.out.println();
        System.out.println("使用Unicode打印\\u4e2d的char值:"+'\u4e2d');
    }
    public static void main(String[] args) throws UnsupportedEncodingException {
        //打印‘中’在utf-8,unicode,gbk中的不同编码值
        String a = "中";
        System.out.print("\nutf-8编码转换为16进制后:");
        byte[] bs1 = a.getBytes("UTF-8");
        for(byte b:bs1){
            System.out.print((String)Integer.toHexString(b)+"\t");
        }
        System.out.print("\nGBK编码转换为16进制后:");
        byte[] bs2 = a.getBytes("GBK");
        for(byte b:bs2){
            System.out.print(Integer.toHexString(b)+"\t");
        }
        char c = '中';
        int n = c;
        System.out.print("\nUnicode编码转换为16进制后:"+Integer.toHexString(n));
        System.out.println();
        System.out.println("使用Unicode打印\\u4e2d的char值:"+'\u4e2d');
    }

java语句在结束后逗号的作用 java字符串结束标志_java_14

  • UTF-8容错能力强
单字节的utf-编码首位是0:0xxxxxxx
双字节的utf-编码前2位是11:110xxxxx xxxxxxxx
三字节的utf-编码首位是111:1110xxxx xxxxxxxx xxxxxxxx

Java使用Unicode编码,程序运行时使用Unicode编码

  • 输入输出是把String和byte[]转换,需要考虑编码
  • 始终优先考虑UTF-8编码

总结:

  • 字符串是不可变对象
  • 字符串操作不改变原字符串内容,而是返回新字符串
  • 常用的字符串操作:提取子串、查找、替换、大小写转换等
  • 字符串和byte[]互相转换时要注意编码,建议总是使用utf-8编码