短信拆分的编写

public String splitMessage(String mobile,String message) {
    
        String result = "failure";
        
        int lengthMessage = message.length();
        int sizeMessage = 1;
        //后缀带【XXX】占据了5个字符
        int typeFlag = (lengthMessage != message.getBytes().length) ? 65 : 135;
        
        if (lengthMessage > typeFlag) {
            sizeMessage = lengthMessage / typeFlag;
            sizeMessage = ((lengthMessage % typeFlag) == 0) ? sizeMessage
                    : (sizeMessage + 1);
        }
        System.out.println(sizeMessage);
        System.out.println(typeFlag);
        for (int i = 0; i < sizeMessage; i++) {

            if (i == (sizeMessage - 1)) {
            System.out.println(mobile);
                System.out.println(message.substring(i * typeFlag, lengthMessage));
            result = SupplierOfMACT.sendSMS(mobile, message.substring(i * typeFlag, lengthMessage));
            } else {
                System.out.println(mobile);
            System.out.println(message.substring(i * typeFlag, (i + 1) * typeFlag));
            result = SupplierOfMACT.sendSMS(mobile, message.substring(i * typeFlag, (i + 1) * typeFlag));
             }
        }

        return result;
    }

很灵活的计算,开眼界了

posted @ 2015-07-06 15:44  jimw  阅读(330)  评论(0编辑  收藏  举报