hi,
i am writing a code sending a messages to mobiles, my aim is if user enter morethan 150 characters that time 0 to 150 characters are send to one message. and remaing between 151 to 300 characters are send to 2nd message.
I write substring like:
string msg=TxtMessage.Text;
int count = msg.Length;
int x = 0;
while (x <= count)
{
string imsg = msg.Substring(x, 150);
// Send message logic here
x += 150;
}
using this it work only if user enter 150 or 300 or 450 characters. if user enter 50 0r 200 etxc characters it is not work. it display error that is., "Index and length must refer to a location within the string. Parameter name: length". i understand this error message,
but how to write the substring function using this type of application. my aim is one message send lessthan 151 characters(0 to 150) and 2nd message send between 151 to 300 characters.
Please give a code todo this.
Thanks.
Loading

Sagar Kumar8 reputationPosted May 4, 2010, 11:49 PM EDT
if lenth >150 then only do it rest of the thing is same.
but in else send the message and break the whilw loop.
Jaish MathewsRank #225 · 11K reputationPosted May 5, 2010, 12:14 AM EDT
public static void Main()
{
//string msg = new String('a',310);
string msg = new String('a', 14);
int subStrLength = 150;
int count = msg.Length;
int x = 0;
while (x < count)
{
// string imsg = msg.Length <= 150 ? msg : msg.Substring(x, 150);
string imsg = msg.Length <= 150 ? msg : msg.Substring(x, subStrLength);
// Send message logic here
x += 150;
subStrLength = count - x;
}
}
CrishRank #1,486 · 1.1K reputationPosted May 5, 2010, 2:30 AM EDT
add one line logic in your code. just check message length if length greater than 150 then use your code else don't need to do substring.
thanks
ajay rajuRank #4,639 · 333 reputationPosted May 12, 2010, 9:21 PM EDT
please once explain this line
string msg = new String('a', 14);
Thanks.