I am looking to split a string into multiple strings at 160 characters I thought easy enough var splits = myString.split(160);
but apparently that doesn't work anyway.
The other point is that I want to add a counter (android sms style).
so lets say we have this example string (237 characters)
hi there, this message is 237 characters long, which makes it much to long for a single message, this string will be split up into multiple messages... this is actually for an sms application hence the reason we need to split the string.
The final output should be
hi there, this message is 237 characters long, which makes it much to long for a single message, this string will be split up into multiple messages... thi(1/2)
s string will be split up into multiple messages... this is actually for an sms application hence the reason we need to split the string.(2/2)
Now if there was always going to be 9 or less messages I could just do
//ok, so the next line won't work, but it gets the point across...
var splits = mystring.split(155);
var s = splits.length;
for(var i = 0; i < s; i++)
splits[i] += '('+(i+1)+'/'+s+')';
but the issue is that there could be anywhere up to 15 messages, so the amount of characters appended on the end is inconsistent (we want to keep the character count as low as possible to 0 padding numbers less than 10 is not an option).
How can I do this?
I am looking to split a string into multiple strings at 160 characters I thought easy enough var splits = myString.split(160);
but apparently that doesn't work anyway.
The other point is that I want to add a counter (android sms style).
so lets say we have this example string (237 characters)
hi there, this message is 237 characters long, which makes it much to long for a single message, this string will be split up into multiple messages... this is actually for an sms application hence the reason we need to split the string.
The final output should be
hi there, this message is 237 characters long, which makes it much to long for a single message, this string will be split up into multiple messages... thi(1/2)
s string will be split up into multiple messages... this is actually for an sms application hence the reason we need to split the string.(2/2)
Now if there was always going to be 9 or less messages I could just do
//ok, so the next line won't work, but it gets the point across...
var splits = mystring.split(155);
var s = splits.length;
for(var i = 0; i < s; i++)
splits[i] += '('+(i+1)+'/'+s+')';
but the issue is that there could be anywhere up to 15 messages, so the amount of characters appended on the end is inconsistent (we want to keep the character count as low as possible to 0 padding numbers less than 10 is not an option).
How can I do this?
Share edited Aug 10, 2011 at 22:58 Hailwood asked Aug 10, 2011 at 16:18 HailwoodHailwood 92.8k112 gold badges273 silver badges425 bronze badges 2- @naveen: was using the wrong function really worth a -1 ? a simple ment would suffice... – Hailwood Commented Aug 10, 2011 at 22:59
- 1 Cheers, we all have those days ;) – Hailwood Commented Aug 11, 2011 at 0:27
2 Answers
Reset to default 3http://jsfiddle/Lobstrosity/nwaYe/
It will cut off at 160 * 15 characters (since you implied that 15 was the limit). It sets both of those numbers as variables up top so you can fiddle with either one.
Update
New fiddle: http://jsfiddle/Lobstrosity/nwaYe/2/
It's ugly...but it works...
- It figures out if the
y
in(x/y)
is going to be one digit or two. - It uses a placeholder in the indicators while it builds up the actual message.
- It replaces the placeholders.
- Finally, it splits on
charLimit
.
I hope someone else figures out a cleaner way to do this...
I think your best choice for getting optimal messages (i.e. max length) may be to handle each class of max message length separately.
Handle the cases where you have less than ten messages with your current code, then extend that method to cover cases where you have up to 99 messages. If there's a chance of going over 100 messages, then you could extend further, but that sounds unlikely.
Here's a code sample:
if (mystring.length < (155 * 9)) {
var splits = mystring.split(155);
var s = splits.length;
for(var i = 0; i < s; i++)
splits[i] += '('+(i+1)+'/'+s+')';
} else if (mystring.length < (154*9)+(153 * 90)) {
var splits1 = mystring.substr(0,154*9).split(154);
var splits2 = mystring.substr(154*9).split(153);
var splits = [];
var s = splits1.length + splits2.length;
for (var i = 0; i < splits1.length; i++)
splits[i] = splits1[i] + '('+(i+1)+'/'+s+')';
for (var i = 10; i < splits2.length+10; i++)
splits[i] = splits2[i-10] + '('+(i+1)+'/'+s+')';
}