刚刚复习了下String基础方法,格式化还没理解清楚。

本文详细介绍Java中字符串的各种操作方法,包括连接、长度获取、字符获取、大小写转换、分割、子串获取及替换等,并通过具体示例展示了每种方法的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package StringTest;

public class StringTest1
{
	public static void main(String[] args)
	{
		//连接字符串cancat()。
		String s = new String("hello");
		String ss = new String("world");
		String sss;
		System.out.println(sss = s.concat(ss));
		//结果为:helloworld。
		//获取sss的字符串长度。
		int v = sss.length();
		//打印sss.length-1长度位置的字符
		System.out.println(sss.charAt(v-1));
		//结果为:d
		System.out.println("------------------");
		String s1 = new String("hello");
		//获取字符串中指定位置的字符charAt();
		System.out.println(s1.charAt(0));
		//结果为:h
		System.out.println("------------------");
		//改变字符串大小写,吧s1中的字符串全部改为大写。
		System.out.println(s1.toUpperCase());
		//结果为:HELLO
		System.out.println("------------------");
		//分割字符串,例如在e位置分割字符串,先声名一个字符串数组接受分割后的元素。
		String[] s2 = s1.split("e");
		System.out.println(s2.length);
		//结果:2
		//迭代器遍历S2
		for(String value:s2)
		{
			System.out.println(value);
		}
		//结果为 h llo,如果分割的是l,那么结果就是he o,两个l全部没了。
		System.out.println("------------------");
		//获取子串
		String a = new String("hello");
		String subs1 = a.substring(0, 2);
		System.out.println(subs1);
		//结果为"he"
		String subs2 = a.substring(2);
		System.out.println(subs2);
		//结果为"loo"
		System.out.println("------------------");
		//更改字符串中部分字符的方法
		//replace(char,char) replaceAll(String,String) replace(String,String)
		String s3 = new String("hello");
		//把"l" 全部替换成 "o"。
		String replacesult = s3.replace("l", "o");
		System.out.println(replacesult);
		//结果为:heooo。
		//将"ll" 统一替换成"LL"。
		String s4 = new String("hello");
		String replacesult1 = s4.replace("ll", "LL");
		System.out.println(replacesult1);
		//结果为:heLLo。
		//将"l"与"LL"替换
		String s5 = new String("hello");
		String replacesult2 = s5.replace("l", "LL");
		System.out.println(replacesult2);
		//结果为:heLLLLo。
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值