17、Java中字符串String

一 String

1.1 String 类

java.lang.String类代表不可变的字符序列

 

1.1.1 String类的常见构造方法:

  • String():空构造

  • String(byte[] bytes):通过使用平台默认的字符集解码指定的byte数组,构造一个新的String

  • String(byte[] bytes, Charset charset):通过使用指定的charset解码指定的byte数组,构造一个新的String

  • String(byte[] bytes, int offset, int length):通过使用平台默认的字符集解码指定的byte数组,并指定数组的位移和长度,构造一个新的String

  • String(byte[] bytes, int offset, int length, String charsetName):通过使用指定的字符集编码指定的byte数组,指定数组的位移和长度,构造一个新的String

  • String(byte[] bytes, String charsetName):通过使用指定的charset解码指定的byte数组,构造一个新的String

  • String(String original):创建一个String对象为original的拷贝

  • String(char[] value):用一个字符数组创建一个String对象

  • String(char[] value,int offset,int count):用一个字符数组从offset项开始的count个字符序列创建一个String对象

对于更多的构造方法,查看API文档

 

 

1.1.1.1 例如下面一段代码

public class TestString {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "world";
        String s3 = "hello";
        System.out.println(s1 == s3);
    }
}

 

打印为true,可以通过内存图进行分析

String类型的值保存在data seg区域中,在JVM中,对于相同的String类型,只要其字符序列相同,则共享内存区域。如下图所示:

所以其打印为true

 

 

1.1.1.2 再如下面一段代码

public class TestString {
    public static void main(String[] args) {
        s1 = new String("hello");
        s2 = new String("hello");
        System.out.println(s1 == s2);
        System.out.println(s1.equals(s2));
    }
}

现在s1和s2是一个对象,不是存在于Data Seg区域了,而是存在于Heap区域中,所以两个对象不会相等。而equals()方法,可有查看API文档,看看String类有没有重写这个方法。

通过查看可以得知,String类重写了Object的equals方法,方法定义为:将此字符串与指定的对象比较,当且仅当该参数不为null,并且是与此对象表示相同字符序列String对象是,结果为true

所以s1.equals(s2)为true

 

 

1.1.1.3 再入下面一段代码

public class TestString {
    public static void main(String[] args) {
        char[] c = {'s','u','n',' ','j','a','v','a'};
        String s4 = new String(c);
        String s5 = new String(c,4,4);
        System.out.println(s4);
        System.out.println(s5);
    }
}

可以看出String s5 = new String(c,4,4);可以查看API文档,发现其构造方法

 

public String(char[] value,int offset,int count)

表示分配一个新的字符数组,offset表示从该数组从哪个长度开始,count表示显示数组的长度。

如上面的String s5 = new String(c,4,4),表示从c字符数组的第4个字符开始,向后4个字符,则表示为java

 

1.1.2 String类常用方法

  • public char charAt(int index):返回字符串中第index个字符

  • int compareTo(String anotherString):按字典顺序比较两个字符串

  • int compareToIgnoreCase(String str):按照字段书序比较两个字符串,不考虑大小写

  • String concat(String str):将指定字符串连接到此字符串的结尾

  • String copyValueOf(char[] data):返回指定数组中表示该字符串序列的String

  • public int length():返回字符串长度

  • public int indexOf(String str):返回字符串中出现str的第一个位置

  • public int indexOf(String str,int formIndex):返回字符串中从fromIndex开始出现str的第一个位置

  • public boolean equalsIgnoreCase(String another):比较字符串与another是否一样(忽略大小写)

  • public String replace(char oldChar,char newChar):在字符串中用newChar字符替换oldChar字符

  • public boolean startsWith(String prefix):判断字符串是否以prefix字符串开头

  • public boolean endsWith(String suffix):判断字符串是否以suffix字符串结尾

  • public String toUpperCase():返回一个字符串为该字符串大写形式

  • public String toLowerCase():返回一个字符串为该字符串的小写形式

  • public String substring(int beginIndex):返回该字符串从beginIndex开始到结尾的子字符串

  • public String substring(int beginIndex,int endIndex):返回该字符串从beginIndex开始到endIndex结尾的子字符串

  • public String trim():返回将该字符串去掉开头和结尾空格后的字符串

  • byte[] getBytes():将String编码为byte数组

  • byte[] getBytes(String charsetName):将String编码为byte数组,指定字符编码

  • char[] toCharArray():将字符串变成字符数组

  • String[] split(String regex):根据匹配给定的正则表达式来拆分字符串

  • String trim():返回字符串的副本,忽略前导空白和尾部空白

  • static String valueOf(boolean b):返回boolean参数的字符串表示形式。下同

  • static String valueOf(char c)

  • static String valueOf(char[] data)

  • static String valueOf(double d)

  • static String valueOf(int i)

  • static String valueOf(float f)

  • static String valueOf(long l)

  • static String valueOf(Object obj)

 

例如下面一段代码

public class TestString2 {
    public static void main(String[] args) {
        String s1 = "sun java";
        String s2 = "Sun Java";

        System.out.println(s1.charAt(4));
        System.out.println(s2.length());
        System.out.println(s1.indexOf("java"));
        System.out.println(s1.indexOf("Java"));
        System.out.println(s1.equals(s2));
        System.out.println(s1.equalsIgnoreCase(s2));

        String s = "我是一个程序员,我在学习java";
        String sr = s.replace('我','你');
        System.out.println(sr);

        String s3 = "Welcome to Java World!";
        String s4 = "    sun java     ";
        System.out.println(s3.startsWith("Welcome"));
        System.out.println(s3.endsWith("World"));
        System.out.println(s3.toUpperCase());
        System.out.println(s3.toLowerCase());
        System.out.println(s3.substring(7));
        System.out.println(s3.substring(8,10));
        System.out.println(s4.trim());
    }
}

 

 

1.1.3 String类常用方法_1

静态重载方法

  • public static String valueOf(...)可以将基本类型数据转换为字符串,例如

public static String valueOf(double d);

public static String valueOf(int i);

 

方法public String[] split(String regex)可以将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组

 

 

如下面一段代码

public class TestString3 {
    public static void main(String[] args) {
        int i = 123456;
        String str = String.valueOf(i);
        System.out.println("i是" + str.length() + "位数");

        String s = "Marry,F,1996";
        String[] t = s.split(",");
        for (int j=0;j<t.length;j++) {
            System.out.println(t[j]);
        }
    }
}

 

 

1.2 练习

1.2.1 练习1

编写一个程序,输出一个字符串中的大写英文字母数,小写英文字母数以及非英文字母数

public class Practice1 {
    public static void main(String[] args) {
        String s = "AaaaABBBBc^%adfddgsasdOOOaKK_haHA";
        int lCount = 0;
        int uCount = 0;
        int oCount = 0;
        for (int i=0;i<s.length();i++) {
            char c = s.charAt(i);
            if (c >= 'a' && c <= 'z') {
                lCount ++;
            } else if (c >= 'A' && c <= 'Z') {
                uCount ++;
            } else {
                oCount ++;
            }
        }
        System.out.println(lCount);
        System.out.println(uCount);
        System.out.println(oCount);
    }
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值