【学习笔记】---字符串总结

本文详细介绍了Java中的String类,包括其不可变性、常用方法如获取长度、字符、子串,判断功能如内容比较、开头结尾判断,转换功能如转换为字节数组、字符数组,其他功能如替换、比较,以及int与String的相互转换。通过示例代码展示了每个方法的用法,帮助读者深入理解String类的使用。
摘要由CSDN通过智能技术生成

字符串

String类代表字符串。Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。

字符串不变; 它们的值在创建后不能被更改。 字符串缓冲区支持可变字符串。 因为String对象是不可变的,它们可以被共享。

字符串常见方法

1. String类的获取功能

  • public int length(): 获取字符串的长度。
  • public char charAt(int index): 获取指定索引位置的字符
  • public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。
  • public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。
  • public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
  • public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
  • public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
  • public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。
public class Mytest1 {
    public static void main(String[] args) {
        String str = "anAdEfg";
        System.out.println("String类的获取功能");
        //获取字符串的长度
        int length = str.length();
        //获取指定索引位置的字符
        char c1 = str.charAt(0);
        //返回指定字符在此字符串中第一次出现处的索引
        int c2 = str.indexOf('n');
        //返回指定字符串在此字符串中第一次出现的索引
        int c3 = str.indexOf("fg");
        //返回指定字符在此字符串中从指定位置后第一次出现处的索引。
        int c4= str.indexOf('f', 2);
        //返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
        int c5 = str.indexOf("fg", 2);
        //从指定位置开始截取字符串,默认到末尾
        String c6 = str.substring(2);
        //从指定位置开始到指定位置结束截取字符串
        String c7 = str.substring(2, 4);
        System.out.println(length);
        System.out.println(c1);
        System.out.println(c2);
        System.out.println(c3);
        System.out.println(c4);
        System.out.println(c5);
        System.out.println(c6);
        System.out.println(c7);
    }
    }
//运行结果:
//7
//a
//1
//5
//5
//5
//AdEfg
//Ad

2. 常见String类的判断功能

  • public boolean equals(Object obj): 比较字符串的内容是否相同,区分大小写
  • public boolean equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写
  • public boolean contains(String str): 判断字符串中是否包含传递进来的字符串
  • public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头
  • public boolean endsWith(String str): 判断字符串是否以传递进来的字符串结尾
  • public boolean isEmpty(): 判断字符串的内容是否为空串""。
public class Mytest1 {
    public static void main(String[] args) {
       String str1 = "axcde";
        String str2 = "Axcde";
        System.out.println("String类的判断功能");
        //比较字符串的内容是否相同,区分大小写
        boolean b = str1.equals(str2);
        //比较字符串的内容是否相同,忽略大小写
        boolean b1 = str1.equalsIgnoreCase(str2);
        //判断字符串中是否包含传递进来的字符串
        boolean b2 = str1.contains("cde");
        //判断字符串是否以传递进来的字符串开头
        boolean b3 = str1.startsWith("ax");
        //判断字符串是否以传递进来的字符出结尾
        boolean b4 = str2.endsWith("de");
        //判断字符串的内容是否为空
        boolean b5 = str1.isEmpty();
        System.out.println(b1);
        System.out.println(b2);
        System.out.println(b3);
        System.out.println(b4);
        System.out.println(b5);
    }
    }
//运行结果:
//String类的判断功能
//true
//true
//true
//true
//false

3. 常见String类的转换功能

  • public byte[] getBytes(): 把字符串转换为字节数组。
  • public char[] toCharArray(): 把字符串转换为字符数组。
  • public static String valueOf(char[] chs): 把字符数组转成字符串。
  • public static String valueOf(int i): 把int类型的数据转成字符串。(String类的valueOf方法可以把任意类型的数据转成字符串。)
  • public String toLowerCase(): 把字符串转成小写。
  • public String toUpperCase(): 把字符串转成大写。
  • public String concat(String str): 把字符串拼接。
public class Mytest1 {
    public static void main(String[] args) {
        String str = "anAdEfg";
        String str1 = "axcde";
        int a=123323;
        //把字符串转换为字节数组
        byte[] bytes = str.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.print(bytes[i]+" ");
        }
        //把字符串转换为字符数组
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            System.out.print(chars[i]+" ");
        }
        System.out.println();
        //把字符数组转换成字符串
        String s2 = new String (chars);
        //把int类型的数据转成字符串
        String s3 = Integer.toString(a);
        //把字符串转换成小写
        String s = str.toLowerCase();
        //把字符串变成大写
        String s1 = str.toUpperCase();
        //字符串拼接
        String s4 = str.concat(str1);
        System.out.println(s);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }
    }
//运行结果:
//97 110 65 100 69 102 103 a n A d E f g 
//anadefg
//ANADEFG
//anAdEfg
//123323
//anAdEfgaxcde

4. 常见String类的其他常用功能

  • public String replace(char old,char new) 将指定字符进行互换
  • public String replace(String old,String new) 将指定字符串进行互换
  • public String trim() 去除两端空格
  • public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果,如果前面几个字母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果,如果连个字符串一摸一样 返回的就是0
  • public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较
package org.baidu2;

public class Mytest1 {
    public static void main(String[] args) {
        String str = "  anAdEfg  ";
        String str1 = "axcde";

        //将指定字符进行互换
        String s = str.replace('a', 'b');
        System.out.println(s);
        //将指定字符串进行互换
        String s1 = str.replace("ab", "qq");
        System.out.println(s1);
        //去除两端空格
        String s2 = str.trim();
        System.out.println(s2);
        //通过字典顺序去比较 返回的值是 ASCII 码的差值  调用者减去传入者
        int i = "abc".compareTo("ABc");
        System.out.println(i);
        //如果前面几个字母一样会根据两个字符串长度进行减法运算返回该减法的结果 (通过长度去比)
        int i1 = "abc".compareTo("a");
        System.out.println(i1);
        //忽略大小写的比较
        int i2 = "abc".compareToIgnoreCase("ABC");
        System.out.println(i2);
    }
    }
//运行结果:
//bnAdEfg  
//anAdEfg  
//anAdEfg
//32
//2
//0

5. 常见String类中int与String的相互转换

int转String有三种方式

  1. num + " "
  2. String.valueOf(num)
  3. Integer.toString(num)

String转int有两种方式

  1. Integer.parseInt(str)
  2. Integer.valueOf(str).intValue()
package org.westos.demo3;

public class mytest6 {
    public static void main(String[] args) {
        //int->String
        int i = 1234567;
        String s = "";
        s = i + "";//会产生两个String对象
        System.out.println(s);
        String s1 = String.valueOf(i);//使用String类的静态方法,只产生一个String对象
        System.out.println(s1);
        String s2 = Integer.toString(i);
        System.out.println(s2);
        //String->int
        String str = "12345";
        int i1 = Integer.parseInt(str);//直接使用静态方法,不会产生多余的对象
        System.out.println(i1);
        int i2 = Integer.valueOf(str).intValue();
        System.out.println(i2);

    }
}
//运行结果:
//1234567
//1234567
//1234567
//12345
//12345

6. Java中的charAt()方法

java.lang.String.charAt() 方法返回指定索引处的char值。索引范围是从0到length() - 1。 声明java.lang.String.charAt()方法。

public class Mytest2 {
    public static void main(String[] args) {
        //统计字符串中的小写字母,大写字母数字空格的个数
        String str = "1123ahdiASDFGF    shaid";
        int upper = 0;
        int lower = 0;
        int num = 0;
        int space = 0;
        for (int i = 0; i < str.length(); i++) {
            //返回该索引处的char值
            char c = str.charAt(i);
            System.out.print(c+" ");
            if(c>'a'&&c<'z'){
                lower++;
            }
            if(c>'A'&&c<'Z'){
                upper++;
            }
            if(c>'0'&&c<'9'){
                num++;
            }
            if(c==' '){
                space++;
            }
        }
        System.out.println();
        System.out.println(lower);
        System.out.println(upper);
        System.out.println(num);
        System.out.println(space);
    }
}
//运行结果:
//1 1 2 3 a h d i A S D F G F         s h a i d 
//7
//5
//4
//4

字符缓冲

public class Test {
	public static void main(String[] args) {
		String str = "helloworld";
		String str1 = "hello";
		String str2 = "world";
        /**
        *jvm做了自动优化,用引用名进行拼接时,jvm会自动调用StringBuilder类的append方法进行拼接,结果存在堆里
        *所以和str的地址不同
        */
		System.out.println(str==(str1+str2));//false
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值