栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java字符串操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java字符串操作

package threeday;
import java.util.Scanner;
import java.util.Date;

public class string_practice {
    public static void main(String args[]){
        //one();
        //two();;
        //three();
        //four();
        //five();
        six();
    }

    public static void one(){
        char s[] = {'a','b','f','r'};
        String a =  new String(s);
        String b = new String(s,1,3);
        System.out.println(a);
        System.out.println(b);

        String s1,s2;
        s1 = "jldfj  dljf fjaelw";
        s2 = "jfk cnjdk ckka";
        System.out.println(s1 +'n' + s2);

        String str1 = new String("hello");
        String str2 = new String("lily");
        String str3 = str1 + ' ' + str2;
        System.out.println(str3);

        int book = 3;
        float price = 1.36f;
        double price_count = 6.546d;
        System.out.println("书籍:" + book + "价格:" + price + "总价:" + price_count);
        int size = str1.length();
        System.out.println(size);
    }

    public static void two(){
        String a = "1579464648";
        String b = "67898632565";
        boolean c = a.startsWith("4");
        boolean d = a.startsWith("15");
        boolean e = b.endsWith("65");
        System.out.println(c );
        System.out.println(d);
        System.out.println(e);
    }

    public static void three(){
        String str = "198.26.32.1";
        String[] str1 = str.split("\.");
        String[] str2 = str.split("\.",2);
        String[] str3 = str.split("\.",3);
        System.out.println("[" + str + "]");
        for (String a: str1) {
            System.out.print("[" + a + "]");
        }
        System.out.println();
        for (String a : str2) {
            System.out.print("[" + a + "]");
        }
        System.out.println();
        for (String a:str3) {
            System.out.print("[" + a + "]");
        }
        System.out.println();
    }

    public static void four(){
        Date date = new Date();
        String year = String.format("%tY",date);
        String month = String.format("%tm",date);
        String day = String.format("%td",date);
        String hour = String.format("%tH",date);
        String minute = String.format("%tM",date);
        String second = String.format("%tS",date);
        System.out.println(year + "." + month + "." + day + " " + hour + ":" + minute + ":" + second);
    }

    public static void five(){
        String regex = "\w+@\w+(\.\w{2,3})*\.\w{2,3}";
        String str1 = "aaa@";
        String str2 = "hfej@.vom";
        String str3 = "hjvbcmn@jkfj.com.cn";
        if (str1.matches(regex)){
            System.out.println("yeas1");
        }
        if (str2.matches(regex)) {

            System.out.println("yeas2");
        }
        if (str3.matches(regex)){
            System.out.println("yeas3");
        }
    }

    public static void six(){
        String str = "";
        long starTime = System.currentTimeMillis();
        for (int i = 0; i < 10000 ; i++){
            str = str + i;
        }
        System.out.println(str);
        long endTime = System.currentTimeMillis();
        long time = endTime - starTime;
        System.out.println(time);

        StringBuilder builder = new StringBuilder("");
        starTime = System.currentTimeMillis();
        for (int j = 0; j < 10000;j++){
            builder.append(j);
        }
        System.out.println(builder);
        endTime = System.currentTimeMillis();
        time = endTime - starTime;
        System.out.println(time);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/724373.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号