JAVA常见对象String类

String类

        代表字符串,JAVA程序中所有字符串字面值(如"abc")都作为此类的实例实况,String是final修饰的所以不能有子类,他们的值在创建后不能更改

        String类重写了toString方法返回的是该对象本身 ,

当def赋值给str原来的abc就变成了垃圾

构造方法

public String();//空构造
public String(byte[] bytes)//把字节数组转成字符串
public String(byte[] bytes,int index,int length)//把字节数组的一部分转成字符串
public String(char[] value)//把字符数组转成字符串
public String(char[] value,int index,int count)//把字符数组的一部分转成字符串
public String(String original)//把字符串常量转成字符串

       

package com.String;

public class demo1_ {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s1 = new String();
		System.out.println(s1);
		
		
		byte[] arr1 = {99,98,97};
		String s2 = new String(arr1);    //解码,将计算机读得懂的转换成我们读的懂的
		System.out.println(s2);
		
		
		byte[]arr2 = {97,98,99,100,101};
		String s3 = new String(arr2,2,3);   //将arr2从2索引开始转换3个
		System.out.println(s3);
		
		char data[] = {'a','b','c'};
		String str = new String(data); //将字符数组转换成字符串
		System.out.println(str);
		
		char[] arr3 = {'a','b','c','d','e'};
		String s4 = new String(arr3,2,3);
		System.out.println(s4);
		
		String s5 = new String("123");
		System.out.println(s5);
		
		
	}

}

 String类的判断功能


         * boolean equals(object obj):比较字符串的内容是否相同,区分大小写
         * boolean equalsIgnoreCase(String str):比较字符串内容是否相同忽略大小写
         * boolean contains(String str):判断大字符串中是否包含小字符串
         * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
         * boolean  ends With(String str);判断字符串是否以某个指定的字符串结尾
         * Boolean  isEmpty():判断字符串是否为空

package com.String;

public class demo2 {
	public static void main(String[] args) {
		/*
		 * boolean equals(object obj):比较字符串的内容是否相同,区分大小写
		 * boolean equalsIgnoreCase(String str):比较字符串内容是否相同忽略大小写
		 * boolean contains(String str):判断大字符串中是否包含小字符串
		 * boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
		 * boolean  ends With(String str);判断字符串是否以某个指定的字符串结尾
		 * Boolean  isEmpty():判断字符串是否为空
		 */
		
		String s1 = "layzhang";
		String s2 = "layzhang";
		String s3 = "Layzhang";
		String s4 = "park";
		String s5 = "yeol";
		String s6 = "park chanyeol";
		
		System.out.println(s1.equals(s2));    //true
		System.out.println(s1.equals(s3));	  //false
		
		System.out.println(s1.equalsIgnoreCase(s2)); //true
		System.out.println(s1.equalsIgnoreCase(s3)); //true
		
		System.out.println(s1.contains(s2));   //true
		
		System.out.println(s6.startsWith(s4)); //true
		
		System.out.println(s6.endsWith(s5));  //true
		
		System.out.println(s6.isEmpty());   //false
		
		
	}

}

String类的获取功能

        * int length();获取字符串长度
         * char charAt(int index);获取指定索引位置的字符
         * int indexOf(int ch);返回指定字符在此字符串中第一次出现处的索引
         * int indexOf(String str);返回指定字符串在此字符串中第一次出现的位置
         * int indexOf(int ch,int fromIndex);返回指定字符在此字符串中从指定位置后第一次出现处的索引
         * int indexOf(String str,int fromIndex);返回指定字符串在此字符串中从指定位置后第一次出现处的索引
         * LastIndexOf
         * String substring(int start);从指定位置开始截取字符串,默认到末尾
         * String substring(int start,int end);从指定位置开始截取字符串到指定位置结束截取字符串

字符串的遍历

public class demo3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		/*
		 * int length();获取字符串长度
		 * char charAt(int index);获取指定索引位置的字符
		 * int indexOf(int ch);返回指定字符在此字符串中第一次出现处的索引
		 * int indexOf(String str);返回指定字符串在此字符串中第一次出现的位置
		 * int indexOf(int ch,int fromIndex);返回指定字符在此字符串中从指定位置后第一次出现处的索引
		 * int indexOf(String str,int fromIndex);返回指定字符串在此字符串中从指定位置后第一次出现处的索引
		 * LastIndexOf
		 * String subString(int start);从指定位置开始截取字符串,默认到末尾
		 * String substring(int start,int end);从指定位置开始截取字符串到指定位置结束截取字符串
		 */
		String s="layzhang";
		
		for(int i=0;i<s.length();i++) {
			System.out.print(s.charAt(i));
			
			//System.out.print(s.substring(i,i+1));
		}
	}

}

        * byte[] getBytes():把字符串转换为字节数组;
         * char[] toCharArray():把字符串转换为字符数组
         * static String valueOf(char[] chs):把字符数组转换成字符串
         * static String valueOf(int i):把int数据类型的数据转换为字符串
         * 
         * 注意:String类中的valueOf方法可以把任意类型的数据转成字符串,底层是由String类的构造方法完成的
         * 
         * String toLowerCase():把字符串转成小写
         * String toUpperCase():把字符串转成小写
         * String concat(String str):把字符串拼接 

public class demo5 {
	public static void main(String[] args) {
		/*
		 * byte[] getBytes():把字符串转换为字节数组;
		 * char[] toCharArray():把字符串转换为字符数组
		 * static String valueOf(char[] chs):把字符数组转换成字符串
		 * static String valueOf(int i):把int数据类型的数据转换为字符串
		 * 
		 * 注意:String类中的valueOf方法可以把任意类型的数据转成字符串,底层是由String类的构造方法完成的
		 * 
		 * String toLowerCase():把字符串转成小写
		 * String toUpperCase():把字符串转成小写
		 * String concat(String str):把字符串拼接
		 * 		 */
		String s1 = "你好你好";
		byte[] arr = s1.getBytes();
		for(int i = 0; i < arr.length; i++) {
			System.out.println(arr[i]);        //-60 -29 -70 -61 -60 -29 -70 -61,GDK码表,中文占两个字节,中文的第一个字节肯定是负数
	}
		String s2 = "abc";
		char[] arr1 = s2.toCharArray();
		for(int i=0;i <arr1.length;i++) {
			System.out.println(arr1[i]);  //a b c
		}
		
		char[] arr3 = {'a','b','c','s'};
		String s3 = String.valueOf(arr3);
		System.out.println(s3);                   //abcs
		
		int i=4;
		String s4 = String.valueOf(i);
		System.out.println(s4);             //4
		
	}
}

String的替换功能

         * 替换功能
         * String replace(char old,char new)
         * String replace(String old,String new)
         * 去除字符串两空格
         * String trim()
         * 按照字典顺序比较两个字符串
         * int compareTo(String str)         
         * int compareToIgnoreCase(String str)  //正数是大于负数是小于等于是0 

public class demo6 {
	public static void main(String[] args) {
		/*
		 * 替换功能
		 * String replace(char old,char new)
		 * String replace(String old,String new)
		 * 去除字符串两空格
		 * String trim()
		 * 按照字典顺序比较两个字符串
		 * int compareTo(String str)         
		 * int compareToIgnoreCase(String str)  //正数是大于负数是小于等于是0
		 * 
		 */
		String s1 = " chan  yeoll ";
		String s2 = s1.replace('l', 'c');
		
		String s3 = s1.replace("ye","er");
		System.out.println(s2);
		System.out.println(s3);
		
		String s4 = s1.trim();
		System.out.println(s4);
		
		
		
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值