1. 程式人生 > >java中的String類 字符串拆分成字符串數組 判定郵箱地址 字符串比較 看結果?

java中的String類 字符串拆分成字符串數組 判定郵箱地址 字符串比較 看結果?

如何 字符串 string style print 拆分 ret bool char

看結果1?

package com.swift;

class ArrayString {
    public static void main(String[] args) {
        String str = "swift:30|sunny:28|Ben:32";
        String str1[] = str.split("\\|");
        for (int i = 0; i <= str1.length - 1; i++) {
            String str2[] = str1[i].split("\\:");

            System.out.println(
"名字是" + str2[0] + "-->" + "年齡是" + str2[1]); System.out.println(); } } }

看結果2?

package com.swift;

class StringEmail 
{
    public static void main(String[] args) 
    {
        String email="[email protected]";
        String email1="[email protected]";
        String email2
="tiankong.sina.com"; String email3="81257211@sina"; String email4="qq.com@81257211"; String email5="@."; System.out.println(operate(email)); System.out.println(operate(email1)); System.out.println(operate(email2)); System.out.println(operate(email3)); System.out.println(operate(email4)); System.out.println(operate(email5)); }
public static boolean operate(String str) { boolean flag=true; if (str.indexOf("@")==-1) { flag=false; } if (str.indexOf(".")==-1) { flag=false; } if (str.indexOf(".")<=str.indexOf("@")) { flag=false; } return flag; } }

看結果3?

package com.swift;

class StringEquals 
{
    public static void main(String[] args) 
    {
        String str="Hello";
        String str1=new String("Hello");
        if(str.equals(str1))
        System.out.println("111111111");
        else
        System.out.println("00000000000");
    }
}

看結果4?

package com.swift;

public class StringResult {
    String str = new String("good");
    char[] ch = { ‘a‘, ‘b‘, ‘c‘ };

    public static void main(String args[]) {
        StringResult sr = new StringResult();
        sr.change(sr.str, sr.ch);
        System.out.print(sr.str + "and");
        System.out.print(sr.ch);
    }

    public void change(String str, char ch[]) {
        str = "test ok";
        ch[0] = ‘g‘;
    }
}

看結果5?

package com.swift;

class StringJudge 
{
    public static void main(String[] args) 
    {
        String str1="Hello";
        String str2=new String(" World");
        System.out.println(str1+str2);
        String a="ok";
        String b="ok";
        String c=new String ("ok");
        if(a==b)
            System.out.println("1");
        else
            System.out.println("0");
        if(a==c)
            System.out.println("1");
        else
            System.out.println("0");
        if(b==c)
            System.out.println("1");
        else
            System.out.println("0");
        
        if(a.equals(b))
            System.out.println("1");
        else
            System.out.println("0");
        if(a.equals(c))
            System.out.println("1");
        else
            System.out.println("0");
        if(b.equals(c))
            System.out.println("1");
        else
            System.out.println("0");
    }
}

如何解釋?

不同的是,第一條先在內存中創建了"ok"這個String,然後將reference賦給a,下一條語句String b = "ok";那麽JVM將不再創建"ok",而是直接將第一個"ok"的reference賦給b,也就是說,a和b是使用同一塊內存,而String c = new String("ok");那JVM將在內存中再創建一塊區域放上“ok”這個字符串。

java中的String類 字符串拆分成字符串數組 判定郵箱地址 字符串比較 看結果?