JavaSE|String的基本功能


String是多个字符组成的一串数据。可以和字符数组进行相互转换。

构造方法

1. public String()
2. public String(byte[] bytes)
3. public String(byte[] bytes,int offset,int length)
4. public String(char[] value)
5. public String(char[] value,int offset,int count)
6. public String(String original)

下面代码块不是构造方法,但是结果也是一个字符串对象。是最常用的方法。

String s = "hello";

String特性

1.字符串一旦被赋值,就不能改变。
PS:指的是字符串的内容不能被改变,而不是引用不能被改变。
eg. String s = “hello”; s += “world”; 问s的结果是多少?
A:Helloworld
在这里插入图片描述

2.字面值作为字符串对象和通过构造方法创建对象的不同。
eg. String s = new String(“hello”);和String s = "hello"的区别?
A:前者创建1(字符串常量池里本身存在"hello",则只需要在堆内存中创建一个String对象)或2(字符串常量池里本身不存在"hello",则需在字符串常量池中创建字符串常量"hello",并且需要在堆内存中创建一个String对象)个对象;后者创建0(字符串常量池里本身存在"hello")或1(字符串常量池里本身不存在"hello",则需在字符串常量池中创建字符串常量"hello")个对象。
在这里插入图片描述

String面试题

			//A:==和equals()
			String s1 = new String("hello");
			String s2 = new String("hello");
			System.out.println(s1 == s2);// false(地址不同)
			System.out.println(s1.equals(s2));// true(值相同)

			String s3 = new String("hello");
			String s4 = "hello";
			System.out.println(s3 == s4);// false(地址不同,s3地址指向堆空间,s4在方法区的字符串常量池)
			System.out.println(s3.equals(s4));// true

			String s5 = "hello";
			String s6 = "hello";
			System.out.println(s5 == s6);// true(地址相同,都指向方法区的字符串常量池)
			System.out.println(s5.equals(s6));// true
			
		    //B:字符串的拼接
			String s1 = "hello";
			String s2 = "world";
			String s3 = "helloworld";
			System.out.println(s3 == s1 + s2);// false(先开空间再相加)
			System.out.println(s3.equals((s1 + s2)));// true

			System.out.println(s3 == "hello" + "world");// true(相加后在常量池找)
			System.out.println(s3.equals("hello" + "world"));// true
  • ==:比较引用类型时比较的是地址值是否相同;比较基本类型时比较的值是否相同。
  • equals():只能比较引用类型,默认比较地址值是否相同。String类重写了该方法,比较的是内容是否相同。
  • 字符串如果是变量相加,先开空间,再拼接。
  • 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否则,就创建。
    System.out.println(s3 == s1 + s2);// false
    System.out.println(s3.equals((s1 + s2)));// true

判断功能

1. boolean equals(Object obj)
2. boolean equalsIgnoreCase(String str)
3. boolean contains(String str)
4. boolean startsWith(String str)
5. endsWith(String str)
6. boolean isEmpty()

获取功能

1. int length()

2. char charAt(int index)

3. int indexOf(int ch)

4. int indexOf(String str)
返回指定字符串在此字符串中第一次出现处的索引。

5. int indexOf(int ch,int fromIndex)
返回指定字符在此字符串中从指定位置后第一次出现处的索引。

6. int indexOf(String str,int fromIndex)
indexOf()系列方法,如果找不到就返回-1;

        String s = "helloworld";
        System.out.println("indexOf:" + s.indexOf('l', 4));
		System.out.println("indexOf:" + s.indexOf('k', 4)); // -1
		System.out.println("indexOf:" + s.indexOf('l', 40)); // -1

7. String substring(int start)
从指定位置开始截取字符串,默认到末尾。包含start这个索引。

8. String substring(int start,int end)
从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引。

		String s = "helloworld";
        System.out.println("substring:" + s.substring(5));  // world
		System.out.println("substring:" + s.substring(0));  // helloworld
		System.out.println("----------------------");


		System.out.println("substring:" + s.substring(3, 8));  // lowor
		System.out.println("substring:" + s.substring(0, s.length()));  //helloworld

转换功能

1. byte[] getBytes()
2. char[] toCharArray()
3. static String valueOf(char[] chs)
4. static String valueOf(int i)
5. String toLowerCase()
6. String toUpperCase()
7. String concat(String str)

其他功能

替换功能

1.String replace(char old,char new)
2.String replace(String old,String new)
此方法中不要求old的长度与new相同,例如:

        String s1 = "helloworld";
		String s2 = s1.replace('l', 'k');
		String s3 = s1.replace("owo", "ak47");
		System.out.println("s1:" + s1); //helloworld
		System.out.println("s2:" + s2); //hekkoworkd
		System.out.println("s3:" + s3); //hellak47rld

去除字符串两端空格

String trim()
此方法只去除字符串两端空格,其余位置的空格不会去除。

按字典顺序比较两个字符串

1. int compareTo(String str)
2. int compareToIgnoreCase(String str)

        String s6 = "hello";
		String s7 = "hello";
		String s8 = "abc";
		String s9 = "xyz";
		System.out.println(s6.compareTo(s7));// 0
		System.out.println(s6.compareTo(s8));// 7(h的ASCII减去a的ASCII)
		System.out.println(s6.compareTo(s9));// -16(h的ASCII减去x的ASCII)

String案例

  • 模拟用户登录
  • 字符串遍历
  • 统计字符串中大写,小写及数字字符的个数
  • 把字符串的首字母转成大写,其他小写
  • 把int数组拼接成一个指定格式的字符串
  • 字符串反转
  • 统计大串中小串出现的次数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值