Java中的字符串及其中的常用方法

Java中的字符串及其中的常用方法字符串是由多个字符组成的一串数据String的特点:字符串是常量,一旦被创建就不能改变,这是因为字符串的值是存放在方法区的常量池里面,但是引用可以改变。字符串字面值”ab”也可以看成是一个字符串对象。内存图如下(例子)这样可以更好的理解字符串常量在程序运行过程中在内存中的存储情况。1.常见String类的获取功能publicintlength(): 获取字符串的长…

大家好,欢迎来到IT知识分享网。

字符串是由多个字符组成的一串数据
String的特点:

  • 字符串是常量,一旦被创建就不能改变,这是因为字符串的值是存放在方法区的常量池里面,但是引用可以改变。
  • 字符串字面值”ab”也可以看成是一个字符串对象。
    内存图如下(例子)
    在这里插入图片描述
    在这里插入图片描述
    这样可以更好的理解字符串常量在程序运行过程中在内存中的存储情况。

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): 从指定位置开始到指定位置结束截取字符串。
package org.baidu2;

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(): 判断字符串的内容是否为空串””。
package org.baidu2;

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): 把字符串拼接。
package org.baidu2;

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有三种方式

  • num + “”
  • String.valueOf(num)
  • Integer.toString(num)
      String转int有两种方式
  • Integer.parseInt(str)
  • 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

5.在学习了字符串以后知道了==和equals()的区别,下面拿代码看一下。

package org.baidu2;

public class Mytest1 {
    public static void main(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        System.out.println(s1.equals(s2));//判断字符串内容
        System.out.println(s1 == s2);//判断字符串引用

    }
}

运行结果为:

true
false

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

  • public charcharAt(int index)
    拿个代码简单了解一下。
package org.baidu2;

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

·

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/10757.html

(0)
上一篇 2024-03-29 10:45
下一篇 2024-03-29 13:00

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信