package com.string;
public class Demo1 {
/**
* @author maco
* @param 2014-04-27
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1,str2,str3,str4;
StringBuffer str5;
char str6;
str1=" we are students";
str2=" we are students";
str3="|we|are|students";
str4="we,are,students";
str5=new StringBuffer("we are students");
System.out.println("对字符串进行拼接函数:"+str5.append(",yes,that is the add part!"));
System.out.println("字符串的长度为:"+str1.length());
System.out.println("字符'a'在str1中的位置:"+str1.indexOf("a"));
System.out.println("字符'e'在str1中最后一次出现的位置:"+str1.lastIndexOf("e"));
System.out.println("str1中第2个字符是:"+str1.charAt(1));/*空格也算是一个字符*/
System.out.println("从str1中截取从2到8的字符:"+str1.substring(3, 8));
System.out.println("去掉空格后的长度:"+str1.trim().length());/*这里的空格是指str1前面或者是后面的空格*/
System.out.println("把str1中的e替换成r"+str1.replace("e", "r"));
System.out.println("字母大小写的转换:"+str1.toLowerCase()+":"+str1.toUpperCase());
/*这里的str1我的是以空格开始,t结束*/
System.out.println("判断str1的开始与结束:"+str1.startsWith("w")+" "+str1.endsWith("t"));
/*比较两个字符串是否相等equals()和compareTo()方法,在compareTo()方法中0表示为相等,1表示为不相等*/
System.out.println("判断两个字符串是否相等的两种比较方法"+str1.equals(str2)+" "+str1.compareTo(str2));
/*字符串的分割*/
String[] newstr1=str4.split(",");
String[] newstr=str3.split("\\|");//以逗号分割,后面的2指定分割次数,以"."和"|"进行分割
for(int i=0;i<newstr1.length;i++)
{
System.out.println(newstr1[i]);
}
System.out.println(newstr.length);
for(int i=0;i<newstr.length;i++)
{
System.out.println(newstr[i]);
}
}
}