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]);//分割后变成了拥有五个数组元素的数组
}
}
java中对常用的字符串的操作 拼接,替换,删除,转换等
最新推荐文章于 2024-07-29 02:08:59 发布