String的常用方法与将金额转换成大写

在使用字符串类型的时候,经常某种方法就忘了,现在整理一下,除了format那个磨磨唧唧的格式化方法。



代码:

/**
 * 描述:
 * 作者: ChengShanyunduo
 * 创建日期: 2017-06-30
 * 修改记录:
 */
public class StringTest {


    public static void main(String[] args) {
        String s = "We are students ";
        String s2 = "WE ARE students ";
        String s3 = "123,456.789,000";

        //字符串长度
        System.out.println("字符串长度:"+s.length());
        //首次出现位置
        System.out.println(s.indexOf("s"));
        //最后出现位置
        System.out.println(s.lastIndexOf("s"));
        //索引位置字符
        System.out.println(7);
        //去除字符串前后空格
        System.out.println(s.trim());
        //去除所有空格,利用替换
        System.out.println(s.replaceAll(" ",""));
        //替换第一次出现的
        System.out.println(s.replaceFirst("s","a"));
        //是否以W开头
        System.out.println(s.startsWith("W"));
        //忽略大小写判断相等
        System.out.println(s.equalsIgnoreCase(s2));
        //全变小写
        System.out.println(s2.toLowerCase());
        //分割字符串
        String[] s3s = s3.split(",|.");
        for ( String a : s3s){
            System.out.println(a);
        }
        //判断是否是数字
        char[] c = s3.toCharArray();
        for (char a : c){
            if(!Character.isDigit(a)){    //如果不是数字
                System.out.println(false);
                break;
            }
        }

    }
}

结果:


然后在书上发现个比较好玩的代码,书上的不全,我也懒得下代码,就自己补全了一下,用着效果还可以。


金额转换成大写


代码:

import java.text.DecimalFormat;
import java.util.Scanner;

/**
 * 描述:
 * 作者: ChengShanyunduo
 * 创建日期: 2017-06-30
 * 修改记录:
 */
public class ConvertMoney {

    private static final String STR_UNIT[] = {"","拾","佰","仟","万","拾","佰","仟","亿"};
    private static final String STR_NUMBER[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        System.out.println("输入一个金额");
        String convert = convert(scan.nextDouble());
        System.out.println(convert);
    }

    public static String convert(double d){
        DecimalFormat df = new DecimalFormat("#0.###");
        String strNum = df.format(d);
        if (strNum.indexOf(".") != -1){   //包含小数点
            String num = strNum.substring(0, strNum.indexOf("."));
            if (num.length()>12){
                System.out.println("数字太大不能转换");
                return "";
            }
        }
        String point = "";
        if (strNum.indexOf(".")!= -1){
            point = "元";
        }else {
            point = "元整";
        }
        String result = getInteger(strNum) +point + getDecimal(strNum);
        if (result.startsWith("元")){   //判断是否以元开头
            result = result.substring(1, result.length());   //把开头的元去掉
        }
        return result;
    }

    /**
     * 小数点前面部分
     * @param num
     * @return
     */
    public static String getInteger(String num){
        if (num.indexOf(".") != -1){
            num = num.substring(0, num.indexOf("."));
        }
        num = new StringBuffer(num).reverse().toString();
        StringBuffer temp = new StringBuffer();
        for (int i=0; i<num.length(); i++){
            temp.append(STR_UNIT[i]);
            temp.append(STR_NUMBER[num.charAt(i)-48]);
        }
        num = temp.reverse().toString();

        while (num.indexOf("零零") != -1 || num.indexOf("零佰") != -1 || num.indexOf("零仟") != -1 || num.indexOf("零万") != -1 || num.indexOf("零亿") != -1 || num.indexOf("亿万") != -1 || num.indexOf("零拾") != -1) {
            num = numReplace(num, "零拾", "零");
            num = numReplace(num, "零佰", "零");
            num = numReplace(num, "零仟", "零");
            num = numReplace(num, "零万", "万");
            num = numReplace(num, "零亿", "亿");
            num = numReplace(num, "零零", "零");
            num = numReplace(num, "亿万", "亿");
            if (num.lastIndexOf("零") == num.length() - 1) {
                num = num.substring(0, num.length() - 1);
            }
        }

        return num;
    }

    public static String numReplace(String num, String oldUnit, String newUnit){
        num = num.replaceAll(oldUnit,newUnit);
        return num;
    }

    /**
     * 小数点后面部分
     * @param num
     * @return
     */
    public static String getDecimal(String num){
        if (num.indexOf(".") != -1){
            num = num.substring(num.indexOf(".")+1);
            StringBuffer temp = new StringBuffer();
            temp.append(STR_NUMBER[num.charAt(0)-48]);
            temp.append("角");
            if(num.length()!=1) {
                temp.append(STR_NUMBER[num.charAt(1) - 48]);
                temp.append("分");
            }
            return  temp.toString();
        }
        return "";
    }
}

结果:


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值