Java String1

import java.util.regex.Pattern;


public class Chapter6_3 
{
    public static void main(String[] args) 
    {
        // StringAndStringBuilder();

        // CharacterUnicode();

        // SBDelete();

        //testEncryptUncrypt();

        test2("31233213");
    }   

    public static void test1()
    {
        // 正则表达式判断指定的变量是否是合法的E-mail地址
        String regex = "\\w{0,}\\@\\w{0,}\\.{1}\\w{0,}";
        String str1 = "aaa@";
        String str2 = "aaaa";
        String str3 = "aaaaa@111.com";
        if(str1.matches(regex))
            System.out.println(str1 + "是一个邮箱地址");
        if(str2.matches(regex))
            System.out.println(str2 + "是一个邮箱地址");
        if(str3.matches(regex))
            System.out.println(str3 + "是一个邮箱地址");
        else
            System.out.println("都不是邮箱地址");


        // 计算文章中汉字的个数
        String text = "明日科技soft";
        int amount = 0;
        for(int i = 0; i < text.length(); i++)
        {
            boolean matches = Pattern.matches("^[\u4E00-\u9FA5]{0,}$", ""
                    + text.charAt(i));
            if(matches)
                amount++;
        }

        System.out.println(text + "中有几个汉字:" + amount + "个");
    }

    public static void StringAndStringBuilder()
    {
        String str = "";
        long startTime = System.currentTimeMillis();
        for(long i = 0; i < 10000; i++)
            str = str + i;
        long endTime = System.currentTimeMillis();
        long time = endTime - startTime;
        System.out.println("String消耗时间:" + time);

        StringBuilder builder = new StringBuilder("");
        startTime = System.currentTimeMillis();
        for(long i = 0; i < 10000; i++)
            builder.append(i);
        endTime = System.currentTimeMillis();
        time = endTime - startTime;
        System.out.println("StringBuilder消耗时间:" + time);
    }

    public static void CharacterUnicode()
    {
        String text = "明日soft";
        char arr[] = text.toCharArray();
        StringBuilder builder = new StringBuilder();
        for(char c : arr)
            builder.append((int)c + " ");
        System.out.println("\"明日soft\"的Unicode码是:");
        System.out.println(builder.toString());
    }

    // 去掉字符串中重复的字符
    public static void SBDelete()
    {
        String s = "命运如同海风-吹着青春的舟,飘摇的,曲折的,渡过了时间的海";
        StringBuilder sb = new StringBuilder(s);
        System.out.println(sb);
        System.out.println("length = " + sb.length());

        for(int i = 0; i < sb.length(); i++)
        {
            for(int j = i + 1; j < sb.length(); j++)
            {
                if(sb.charAt(i) == sb.charAt(j))
                    sb.deleteCharAt(j);
            }
        }
        System.out.println(sb);
        System.out.println("length = " + sb.length());      
    }

    // 对value加密,secret密文字符
    public static String EncryptUncrypt(String value, char secret)
    {
        byte bt[] = value.getBytes();       // 转换为字节数组
        for(int i = 0; i < bt.length; i++)
            bt[i] = (byte)(bt[i] ^ (int)secret);    // 通过异或运算进行加密
        return new String(bt, 0, bt.length);
    }

    public static void testEncryptUncrypt()
    {
        String value = "liangjisheng";
        char secret = '祈';
        System.out.println("加密前:" + value);
        String encrypt = EncryptUncrypt(value, secret);
        System.out.println("加密后:" + encrypt);
        String uncrypt = EncryptUncrypt(encrypt, secret);
        System.out.println("解密后:" + uncrypt);
    }

    // 验证字符串是否是回文
    public static void test2(String str1)
    {
        StringBuilder str2 = new StringBuilder(str1);
        str2.reverse();
        int count = 0;
        for(int i = 0; i < str1.length(); i++)
        {
            if(str1.charAt(i) != str2.charAt(i))
            {
                System.out.println(str1 + "不是回文字符串");
                break;
            }

            if(str1.charAt(i) == str1.charAt(i))
                count++;
        }
        if(count == str1.length())
            System.out.println(str1 + "是回文字符串");
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值