Sunday, March 20, 2011

Splitting Text At A Fixed Length

As part of my message app I had originally been splitting my message text "by hand."  I found a method in the SmsManager that does the job for you.  Again, it's still a good bit of code so here it is.  Hope you find it helpful.

This will split the message into 160 character chunks to include anything up to 160 characters at the end of the message.

protected ArrayList<String> splitMsg(SmsMessage smsMessage) {
        ArrayList<String> smt;
        Pattern p = Pattern.compile(".{1,160}");
        Matcher regexMatcher = p.matcher(smsMessage.getMsgBody());
        smt = new ArrayList<String>();
        while (regexMatcher.find()) {
            smt.add(regexMatcher.group());
        }
        return smt;
    }

No comments:

Post a Comment