首页 > 编程语言 > 详细

java中的String类 字符串拆分成字符串数组 判定邮箱地址 字符串比较 看结果?

时间:2017-11-07 20:20:14      阅读:724      评论:0      收藏:0      [点我收藏+]

看结果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="tiankong_0000@sina.com.cn";
        String email1="81257211@qq.com";
        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类 字符串拆分成字符串数组 判定邮箱地址 字符串比较 看结果?

原文:http://www.cnblogs.com/qingyundian/p/7800831.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!