java中对常用的字符串的操作 拼接,替换,删除,转换等

import java.lang.reflect.Array;
import java.util.Arrays;

public class practice {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
           //字符串的创建
	  String s="Hello";
	  String ss=new String("Hello");
	  //截取一个字符
	  char ch;
		ch=s.charAt(2);
		System.out.println(ch);
		//取出子串
		String out=s.substring(3);
		System.out.println(out);
		String out1=s.substring(1,3);
		System.out.println(out1);
		//字符串的拼接
		String con;
		con=s.concat(ss);
		System.out.println(con);
		
		String con1=ss+s;
		System.out.println(con);
		//字符串的比较
		int temp;
		temp=s.compareTo(ss);
		System.out.println(temp);
		
		//字符串与字符数组相互转换
		char [] chh= {'H','e','l' ,'l', 'o'};
		String s2=String.copyValueOf(chh);
		System.out.println(s2);
		
		char chh2[]=new char [5];
		s2.getChars(0, s2.length(), chh2,0);
		for(int i=0;i<s2.length();i++)
		{
		  System.out.print(chh2[i]+" ");	
		}
		System.out.println();
		//去掉起始和结尾的空格
		String s3="  this  is  a  delete space  ";
		String s3Plus=s3.trim();
		System.out.println(s3Plus);
		
		//字符串替换函数
		String s4=s3.replace("is", "love");//也可以用单引号替换单个字符
		System.out.println(s4);//替换后  为 thlove  love  a  delete space  
	
	    //字符串大小写转换
		//小写转换大写  用  .toUpperCase();
		//大写转换小写  用  .toLowerCase();
	    s4=s4.toUpperCase();
	    System.out.println(s4);
	    s4=s4.toLowerCase();
	    System.out.println(s4);
	    
	    
	    //将其他类型转换成字符串
	    String s5=String.valueOf(true);//boolean
	    String s6=String.valueOf(123);//int 
	    String s7=String.valueOf(12.8);//double
	    String s8=Double.toString(12.8);
	    System.out.println(s5);
	    System.out.println(s6);
	    System.out.println(s7);
	    System.out.println(s8);
	     
	    
	    //字符串的分割函数
	    String s9="I will split this String !";
	    String []s9Plus=s9.split(" ");
	    System.out.println(s9Plus[1]);//分割后变成了拥有五个数组元素的数组
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌晨里的无聊人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值