java常用类:String

String类

String是特殊的引用类型 字符串是常量;它们的值在创建之后不能更改。(常量是在方法区中:字符串常量池 ;前提:将字符串常量直接赋值的形式 )

构造方法 :

String() :空参构造

String(byte[] bytes):将字节数组-->String 数据

String(byte[] bytes, int offset, int length):将字节数组的一部分转换字符串

String(char[] value) :将字符数组转换成字符串

String(char[] value, int offset, int length):将字符数组的一部分转换字符串

 String(String original) :创建一个字符串对象:传递字符串常量

 

String s ="" ; String s = null ;区别: 前者:表示当前s对象是一个空内容 后者:表示空对象

 

字符串的特点:

字符串是常量,一旦被赋值,其值不能被更改

内容不能变,引用可以变

 

String s = new String(“hello”)和String s = “hello”的区别

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():                               判断字符串是否为空

equals

public boolean equals(Object anObject)

将此字符串与指定的对象比较。当且仅当该参数不为 null,并且是与此对象表示相同字符序列的 String 对象时,结果才为 true

/**
 * 字符串变量相加,是先开辟空间(不是先相加),在看常量池中是否有这个字符串常量..
 * 字符串常量相加,是先拼接(先相加),在开辟空间
 * @author Administrator
 *
 */
public class StringDemo5 {
	
	public static void main(String[] args) {
		String s1 = "hello";
		String s2 = "world";
		String s3 = "helloworld";
		System.out.println(s3 == s1 + s2);//false
		System.out.println(s3.equals((s1 + s2))) ;//true

		//s==helloworld
		System.out.println(s3 == "hello" + "world");//true
		System.out.println(s3.equals("hello" + "world"));//true
	}
}

==与equals的区别:

1.对于基本数据类型,==比较的是数据的值是否相等,euals只能比较对象

2.对于普通对象,==和equals都是比较的两个对象的地址是否相同

3.对于String:==比较的对象的地址,equals比较的是字符串的内容是否相同,原因:String类重写了equals方法

 

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):        返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
            可以顺带提一下lastIndexOf系列(类似)
        public String substring(int start):                从指定位置开始截取字符串,默认到末尾。
        public String substring(int start,int end):        从指定位置开始到指定位置结束截取字符串。
  

 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 static void main(String[] args) {
        //统计大串中小串出现的次数
        String maxStr = "woaijavawozjavahendeaijavawohenajavaihenaijava";
        String minstr = "java";
        int count =0;
        int index = 0;
        while((index = maxStr.indexOf(minstr)) != -1){
            count ++;
            maxStr = maxStr.substring(index+minstr.length());
           
        }
        System.out.println(minstr+"出现了"+count+"次");;
    }

String的替换功能


        public String replace(char old,char new)            将指定字符进行互换
        public String replace(String old,String new)        将指定字符串进行互换
   

String的去除字符串两空格


        public String trim()    

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值