JAVA String类

String类

1.构造方法

public String() 初始化一个新创建的 String 对象,使其表示一个空字符序列。
public String( byte[] bytes):把字节数组转成字符串
public String( byte[] bytes, int index, int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
public String( char[] value):把字符数组转成字符串
public String( char[] value, int index, int count):把字符数组的一部分转成字符串

public class test {
    public static void main(String[] args) {
        String s = new String();//空参构造,创建了一个对象,不过字符串的内容是个空串
        String s1 = new String("abc");
        byte[] bytes={98,99,100,101};
        String s2 = new String(bytes);//"bcde" 把一个字节数组转成字符串
        String s3 = new String(bytes, 1, 2);//把字节数组一部分转换为字符串
        char[] chs={'a','b',100,'e'};
        String s4 = new String(chs);//将字符数组转换为字符串
        String s5 = new String(chs,0,2);//将字符数组的一部分转换为字符串
    }
}

注意
1.Java 程序中的所有字符串字面值(如 “abc” )都作为String类的实例实现。

 int length = "abc".length();//可以直接使用String类中的方法

2.字符串是常量;它们的值在创建之后不能更改

String s = "hello";
s = "world" + "java";//重写创建一个字符串并把它的值附给s这个引用而不是改变了s字符串的内容。

3.使用new创建字符串会在堆区开放空间,而直接使用“ ”创建字符串只会在方法区的常量池中存储。

2.String类中的方法

1).判断功能方法

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 test{
    public static void main(String[] args) {
        boolean b1 = "abc".equals("Abc");//flase
        boolean b2="abc".equalsIgnoreCase("ABC");//true
        boolean b3="abc".contains("ab");//true
        boolean b4="abc".startsWith("a");//true
        boolean b5="abc".endsWith("b");//flase
        boolean b6=" ".isEmpty();//flase  空格不等于空串
    }
}

2)获取功能方法

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 int lastIndexOf ( int ch):返回指定字符在此字符串中最后一次出现处的索引。
public int lastIndexOf (String str):返回指定字符串在此字符串中最后一次出现处的索引。
public int lastIndexOf ( int ch, int fromIndex):返回指定字符在此字符串中从指定位置后最后一次出现处的索引。
public int lastIndexOf (String str,int fromIndex):返回指定字符串在此字符串中从指定位置后最后一次出现处的索引。
public String substring ( int start):从指定位置开始截取字符串, 默认到末尾。
public String substring ( int start, int end):从指定位置开始到指定位置结束截取字符串。

public class test {
    public static void main(String[] args) {int length = "abc".length();
        System.out.println(length);
		//charAt();
        char c = "像我这样优秀的人,本该灿烂过一生".charAt(1);//根据索引截取字符串中的单个字符
        System.out.println(c);//我
        //indexOf();
        int index= "abcdefdfdfasdbbbfassdf".indexOf('爱');//检索该字符在字符串中第一次出现的索引,如果检索不到返回 -1
            index="abcdsfwefew".indexOf("abcf");
            index="afsfsdfsdf".lastIndexOf("f");//从后往前找该字符串第一次出现的索引
        System.out.println(index);
        //substring();
        String s = "往后余生,洗衣是你,做饭是你,哄娃也是你".substring(0, 4);//含头不含尾
        s= "往后余生,洗衣是你,做饭是你,哄娃也是你".substring(5);
        System.out.println(s);
    }
}

3)转换功能方法

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 test {
    public static void main(String[] args) {
  		  //getBytes():把字符串转换为字节数组。
        byte[] bytes = "我爱你".getBytes();
        for(int i=0;i<bytes.length;i++){
            System.out.println(bytes[i]);
        }
        //toCharArray ():把字符串转换为字符数组。
        char[] chars = "江畔何人初见月,江月何年初照人".toCharArray();
        for(int i=0;i<chars.length;i++){
            System.out.println(chars[i]);
        }
       
       // public static String valueOf (char[] chs):把字符数组转成字符串。
        String s = String.valueOf(true);
        System.out.println(s);
        int num=100;
        System.out.println(num+"");//"100"
        String s1 = String.valueOf(chars);
        System.out.println(s1);
        
        String s2 = "abc".toUpperCase();
        System.out.println(s2);
        String s3 = "ABC".toLowerCase();
        System.out.println(s3);
        System.out.println("--------------------------------");
        String concat = "abc".concat("").concat("abc");
        System.out.println(concat);

    }
}

4)替换功能方法

public String replace ( char old, char new)将指定字符进行互换
public String replace (String old, String new)将指定字符串进行互换
public String trim ()去除字符串两空格
public int compareToIgnoreCase(String);//忽略大小写的比较

public class StringDemo {
    public static void main(String[] args) {
        //String的替换功能及案例演示
        String s = "美国总统奥巴马".replace("奥巴马", "特朗普");
        System.out.println(s);//美国总统特朗普
        String s1 = "美国总统奥巴马现在的总统是特朗普".replace("奥巴马", "*").replace("特朗普", "*");
        System.out.println(s1);//美国总统*现在的总统是*
        //String的去除字符串两空格及案例演示
        //public String trim ()
        String str="   abc     ";
        String trim = str.trim();
        System.out.println(trim);//"abc"
        int i1 = "abc".compareToIgnoreCase("ABC");//忽略大小写的比较
        System.out.println(i1);//1
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值