String 类

String类

String 不可变长的字符序列 “abc”
Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现
其内部是由字符串组表示 private final char value[ ];

String : 不可变长字符串

String:构造器

本质上就是String强转数据
对象|字节数组|字符数组|代码点数组 => 强转字符串


构造器1:

String() 

初始化一个新创建的 String 对象,使其表示一个空字符序列。


构造器2:

String(byte[] bytes) 

通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。


构造器3:

String(char[] value) 

分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。


构造器4:

String(int[] codePoints, int offset, int count) 

分配一个新的 String,它包含 Unicode 代码点数组参数一个子数组的字符。


常用方法:

将一个字符数组 赋给字符串

	String a = new String(new char[] { 'a', 'b', 'c', 'd', 'e' });
		System.out.println(a);// abcde

将一个指定区间的字符数组赋给字符串

	String b = new String(new char[] { 'a', 'b', 'c', 'd', 'e' }, 1, 4);
		System.out.println(b);// bcde

getBytes()
返回值: byte[ ]
使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的byte 数组中。 byte[ ] getBytes(Charset charset)

byte[] c = "abcde".getBytes();
		System.out.println(Arrays.toString(c));
		// [97, 98, 99, 100, 101]
		String cStr = new String(c);
		// 实现字节数组转回字符串
		System.out.println(cStr);
		// abcde

charAt(int index)
返回值: char
返回指定索引处的 char 值

		String string = "abcdefg";
		System.out.println(string.charAt(5));// f

codePointAt(int index)
返回值: int
返回指定索引处的字符(Unicode 代码点)。

System.out.println(string.codePointAt(4));// 101
		

getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin )
返回值: void
将字符从此字符串复制到目标字符数组。

		char[] array = new char[7];
		string.getChars(1, 5, array, 1);
		System.out.println(array);// bcde 前后有空格

★★★
indexOf(String str)
返回值: int
返回指定子字符串在此字符串中第一次出现处的索引

indexOf(String str, int fromIndex)
返回值: int

lastIndexOf(String str)
返回值: int
返回指定子字符串在此字符串中最右边出现处的索引。

	System.out.println(string.indexOf("d"));// 3
	System.out.println(string.indexOf("d", 4));// -1//未找到从索引4处开始查找
	System.out.println(string.lastIndexOf("d"));// 3

compareTo(String anotherString)
返回值: int
按字典顺序比较两个字符串。

compareToIgnoreCase(String str)
返回值: int
按字典顺序比较两个字符串,不考虑大小写。
相同返回0,不同 使用当前字符串与参数字符串同一个位置的字符进行减法

		String string2 = "abcdEfg";
		System.out.println(string.compareTo(string2));// 32
		System.out.println(string.compareToIgnoreCase(string2));// 0

concat(String str)
返回值: String
将指定字符串连接到此字符串的结尾。 返回新串

		System.out.println(string.concat(string2));// abcdefgabcdEfg

contains(CharSequence s)
返回值: boolean
当且仅当此字符串包含指定的 char 值序列时,返回 true。

System.out.println(string.contains("abcde"));

copyValueOf(char[] data)
返回值: static String
字符数组转为字符串

copyValueOf(char[] data, int offset, int count)
返回值: static String

		char[] charArray = { 'q', 'w', 'e', 'r', 't' };
		System.out.println(string.copyValueOf(charArray, 2, 3));// ert

★★★★★
length()
返回值: int
返回字符串长度

		System.out.println(string.length());//7

substring(int beginIndex)
返回值: String
返回一个新的字符串,它是此字符串的一个子字符串。

substring(int beginIndex, int endIndex)
返回值: String

endIndex
返回值: String
不包含指定索引 返回一个新字符串,它是此字符串的一个子字符串。

		System.out.println(string.substring(1, 4));//bcd

★★★
toCharArray()
返回值: char[]
将此字符串转换为一个新的字符数组。

	char[] aaa = string.toCharArray();//

toLowerCase()
返回值: String
字符转小写

toUpperCase()
返回值: String
字符转大写

		System.out.println(string2.toLowerCase());//abcdefg
		System.out.println(string.toUpperCase());//ABCDEFG

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

		System.out.println(string.trim());//abcdefg

valueOf()
返回值: String
参数转为字符串

		System.out.println(string.valueOf(13.1) + 1);//13.11

★★★
replace(char oldChar, char newChar)
返回值: String
新字符串替换旧字符串

		System.out.println(string.replace("abc", "zzz"));//zzzdefg
var foo = 'bar';

split(String regex)
返回值: String[]
根据给定正则表达式的匹配拆分此字符串。★★★★

		String str3 = "nameszhangsan";
		String[] s = str3.split("s");
		System.out.println(Arrays.toString(s));//[name, zhang, an]

split(String regex, int limit)
返回值: String[]
根据匹配给定的正则表达式来拆分此字符串。

// limit 参数控制模式应用的次数,因此影响所得数组的长度。
// 如果该限制 n 大于 0,则模式将被最多应用 n - 1 次,  //裁剪n-1次
// 数组的长度将不会大于 n,而且数组的最后一项将包含所有超出最后匹配的定界符的输入。
// 如果 n 为非正,那么模式将被应用尽可能多的次数,而且数组可以是任何长度。
// 如果 n 为 0,那么模式将被应用尽可能多的次数,数组可以是任何长度,并且结尾空字符串将被丢弃。
		String str4 = "afhfyftofn";
		String[] ss = str4.split("f", 4);
		System.out.println(Arrays.toString(ss));
//[a, h, y, tofn]

StringBuilder & StringBuffer

都添加了append方法
StringBuilder: 可变长字符串,线程不安全的
StringBuffer: 可变长字符串,线程安全的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值