So I'm trying to write a method that will split strings that are longer than 160 by inserting the xml tag . I have tried to use and modify java's wordWrap method. But this seems to split sentences at fullstops or . I do not want this to happen because these strings have ID's in them that show as follows : FID.1262.
The mothod I currently have:
private String tests(String Message) {
StringBuilder text = new StringBuilder(Message);
int size = Message.length();
int size1 = 160;
while (!Message.substring(size1 - 1, size1).equals(" ")) {
size1--;
if (Message.substring(size1 - 1, size1).equals(" ")) {
text.insert(size1, "<SPLIT>");
text.replace(size1 - 1, size1, "");
return "" + text;
}
}
text.insert(size1, "<SPLIT>");
text.replace(size1 - 1, size1, "");
text.trimToSize();
return "" + text;
}
The output: This is a test message that will be split when the while loop iterates backwards through the string. It will then place an xml tag with split in is in the lastspace before 160 characters.
The problem is this method will only split a string once no matter how long it is. What I need is for it to be able to split strings of n size at every 160 or less characters by the space.
if anyone can give me any pointers. This turned out to be much harder than it actually sounds...