1. 程式人生 > >java中String類常用方法、屬性等

java中String類常用方法、屬性等

col clas equal ack length ++ ava eal rgs

package Head18;

public class java09 {

    public static void main(String[] args) {
        String s="hello,where are you from? "
                + "Are you American? do you from china?"
                + " no,I am from Australian";
        //統計空格和"re"字符串
        int n=0,n1=0;
        String s1="";
        
for (int i=0;i<s.length()-2;i++){ String r=s.substring(i, i+2); if (r.equals("re")){ n++; } } for (int i=0;i<s.length()-1;i++){ String r=s.substring(i, i+1); if (r.equals(" ")){ n1++; } }
//替換文本 String s3=s; s3=s3.replaceAll("om", "en"); System.out.println(s3); //分割字符 String a[]=s.split("\\?"); for (int i =0; i <a.length;i++){ System.out.println(a[i]); } //A和a字母調換 String s11[]=new
String[s.length()]; String s2=""; for (int i=0;i<s.length()-1;i++){ s11[i]=s.substring(i,i+1); if (s11[i].equals("A")){ s11[i]="a"; }else if (s11[i].equals("a")) { s11[i]="A"; } s2=s2+s11[i]; } System.out.println(s2); System.out.println(n); System.out.println(n1); } }

java中String類常用方法、屬性等