字符串基本操作


public class Zi_FU_CHUAN {

	// 1.1字符串连接

	public static void stringConcatenation() {
		String message1 = "Hello " + "World!";
		String message2 = "Hello ".concat("World!");
		System.out.println("使用+连接字符串:" + message1);
		System.out.println("使用concat()方法连接字符串:" + message2);
	}

	// 1.1.1使用“+”可以将字符与其他另外I型数据连接。对于数学运算,需要特赐额注意运算符的优先级,例如:

	public static void srtingConcatenation1() {
		System.out.println("1+2="+1+2);//输出结果为1+2=12
		System.out.println("1+2="+(1+2));//输出结果为1+2=3
	}
	
	//1.2获取字符串长度
	
	public static void stringLength()
	{
		String message="So asy we all!";
		System.out.println(message+"长度为:"+message.length());
	}
	
	//1.3获取指定字符的索引位置
	
	public static void stringIndex()
	{
		String message="So say we all!";
		System.out.println("s首次出现的索引值:"+message.indexOf("s"));
		System.out.print("s末次出现的索引值:"+message.lastIndexOf("s"));
	}
	
	//1.4获取指定索引位置的字符
	
	public static void stringIndex1()
	{
		String message="So say we all!";
		System.out.println(message+"的奇数索引字符:");
		for(int i=0;i<message.length();i++)
		{
			if(i%2==1)
			{
				System.out.println(message.charAt(i)+" ");
			}
		}
	}
	
	//1.5字符串比较
	
	public static void stringEquals()
	{
		String message1="mrsoft";
		String message2="mrsoft";
		String message3="Mrsoft";
		System.out.println(message1+"equals"+message2+":"+message1.equals(message2));
		System.out.println(message1+"equals"+message3+":"+message1.equalsIgnoreCase(message3));
	}
	
	//1.5.1比较开头结尾
	
	public static void stringSE()
	{
		String message="So say we all!";
		boolean startsWith=message.startsWith("So");
		boolean endsWith=message.endsWith("all!");
		System.out.println(message+"是以So开头:"+startsWith);
		System.out.println(message+"是以all!结束:"+endsWith);
	}
	
	//1.6字符串替换
	
	public static void stringReplace()
	{
		String message="So say we all!";
		System.out.println("替换前字符串:"+message);
		String replace=message.replace(" ","\n");
		System.out.println("替换后的字符串:"+replace);
	}
	
	//1.7字符串分割
	
	public static void stringSplit()
	{
		String message="So say we all!";
		String[] split=message.split(" ");
		System.out.println(message+"中共有"+split.length+"个单词!");
	}
	
	//大小写转换
	
	public static void stringCase()
	{
		String message="So say we all!";
		System.out.println(message+"转换为大写:"+message.toUpperCase());
		System.out.println(message+"转换为小写:"+message.toLowerCase());
	}
	
	//去除首末空格
	
	public static void stringTrim()
	{
		String message=" So say we all! ";
		System.out.println(message+"的长度为:"+message.length());
		System.out.println("去除首末空格后的长度为:"+message.trim().length());
	}
	public static void main(String [] args)
	{
		stringConcatenation();System.out.println();
		srtingConcatenation1();System.out.println();
		
		stringLength();System.out.println();
		
		stringIndex();System.out.println();
		stringIndex1();System.out.println();
		
		stringEquals();System.out.println();
		
		stringSE();System.out.println();
		
		stringReplace();System.out.println();
		
		stringSplit();System.out.println();
		
		stringCase();System.out.println();
		
		stringTrim();System.out.println();
	}
}

运行结果如下:

使用+连接字符串:Hello World!
使用concat()方法连接字符串:Hello World!


1+2=12
1+2=3


So asy we all!长度为:14


s首次出现的索引值:3
s末次出现的索引值:3
So say we all!的奇数索引字符:




  




mrsoftequalsmrsoft:true
mrsoftequalsMrsoft:true


So say we all!是以So开头:true
So say we all!是以all!结束:true


替换前字符串:So say we all!
替换后的字符串:So
say
we
all!


So say we all!中共有4个单词!


So say we all!转换为大写:SO SAY WE ALL!
So say we all!转换为小写:so say we all!


 So say we all! 的长度为:16
去除首末空格后的长度为:14


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值