JAVA字符串:
//字符串初始化操作
public class Example01 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
//创建一个空的字符串
String str1=new String();
//创建一个内容为abcd的字符串
String str2=new String("abcd");
//创建一个内容为字符数组的字符串
char[] charArray=new char[] {'D','E','F'};
String str3=new String(charArray);
System.out.println("a"+str1+"b");
System.out.println(str2);
System.out.println(str3);
}
}
//String中提供的方法
public class Example02 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String s="abcdedcba";//声明字符串
System.out.println("字符串的长度为:"+s.length());//获取字符串长度,即字符个数
System.out.println("字符串中第一个字符:"+s.charAt(0));
System.out.println("字符c第一次出现的位置:"+s.indexOf('c'));
System.out.println("字符c最后一次出现的位置:"+s.lastIndexOf('c'));
}
}
//字符串转换操作
public class Example03 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String str="abcd";
System.out.println("将字符串转为字符数组后的结果:");
char[] charArray=str.toCharArray();//字符串转换为字符数组
for(int i=0; i<charArray.length; i++) {
if(i !=charArray.length-1) {
//如果不是数组的最后一个元素,在元素后面加逗号
System.out.print(charArray[i]+",");
}else {
//数组的最后一个元素后面不加逗号
System.out.println(charArray[i]);
}
}
System.out.println("将int值转换为String类型之后的结果:"+String.valueOf(12));
System.out.println("将字符串转换成大写之后的结果:"+str.toUpperCase());
}
}
//字符串的替换和去除空格操作
public class Example04 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String s="itcast";
//字符串替换操作
System.out.println("将it替换成cn.it的结果:"+s.replace("it", "cn.it"));
//字符串去除空格操作
String s1=" i t c a s t ";
System.out.println("去除字符串两端空格后的结果:"+s1.trim());
System.out.println("去除字符串中所有空格后的结果:"+s1.replace(" ", ""));
}
}
//字符串的判断操作
public class Example05 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String s1="String";//声明一个字符串
String s2="Str";
System.out.println("判断是否以字符串Str开头:"+s1.startsWith("Str"));
System.out.println("判断是否以字符串ng结尾:"+s1.endsWith("ng"));
System.out.println("判断是否包含字符串tri:"+s1.contains("tri"));
System.out.println("判断字符串是否为空:"+s1.isEmpty());
System.out.println("判断两个字符串是否相等:"+s1.equals(s2));
}
}
/*字符串的截取和分割
* substring()方法用于截取字符串的一部分
* split()方法将字符串按照某个字符进行分割
*/
public class Example06 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
String str="羽毛球-篮球-乒乓球";
//下面是字符串截取操作
System.out.println("从第5个字符截取到末尾的结果:"+str.substring(4));
System.out.println("从第5个字符截取到第6个字符的结果:"+str.substring(4,6));
//下面是字符串分割操作
System.out.print("分割后的字符串数组中的元素依次为:");
String[] strArray=str.split("-");//将字符串转换为字符串数组
for(int i=0; i<strArray.length; i++) {
if(i !=strArray.length-1) {
//如果不是最后一个元素,在元素后面加逗号
System.out.print(strArray[i]+",");
}else {
//数组的最后一个元素后面不加逗号
System.out.println(strArray[i]);
}
}
}
}
/*字符串是常量因此一旦创建,其内容和长度是无法改变的
* 为了便于对字符串的修改,提供了StringBuffer类(字符串缓冲区),其内容和长度都是可以改变的;
* 在操作字符时,如果该字符仅用于表示数据类型,则使用String类即可;需要对字符串进行增删操作则使用StringBuffer类;
* String类覆盖了Object类的equals()方法,而StringBuffer类没有覆盖Object类的equals()方法;
* String s1=new String("abc");
* String s2=new String("abc");
* System.out.println(s1.equals(s2));打印结果为ture
* StringBuffer sb1=new StringBuffer("abc");
* StringBuffer sb2=new StringBuffer("abc");
* System.out.println(sb1.equals(sb2));打印结果为false
* String类对象可以用操作符+进行连接,而StringBuffer类对象之间不能;
*/
public class Example07 {
public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println("1.添加-------------------------------------");
add();
System.out.println("2.删除-------------------------------------");
remove();
System.out.println("3.修改-------------------------------------");
alter();
}
public static void add() {
StringBuffer sb=new StringBuffer();//定义一个字符串缓冲区
sb.append("abcdefg");//在末尾添加字符串
System.out.println("append添加结果:"+sb);
sb.insert(2,"123");//在指定位置插入字符串
System.out.println("insert添加结果:"+sb);
}
public static void remove() {
StringBuffer sb=new StringBuffer("abcdefg");//定义一个字符串缓冲区
sb.delete(1,5);//指定位置删除
System.out.println("删除指定位置结果:"+sb);
sb.deleteCharAt(2);//指定位置删除
System.out.println("删除指定位置结果:"+sb);
sb.delete(0,sb.length());//清除缓冲区
System.out.println("清空缓冲区结果:"+sb);
}
public static void alter() {
StringBuffer sb=new StringBuffer("abcdef");//定义一个字符串缓冲区
sb.setCharAt(1,'p');//修改制定位置字符
System.out.println("append添加结果:"+sb);
sb.replace(1,3,"qq");//替换指定位置字符串或字符
System.out.println("替换制定位置字符(串)结果:"+sb);
System.out.println("字符串翻转结果:"+sb.reverse());
}
}