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
ajay rajuPosted May 13, 2010, 1:21 AM
please once explain this line
string msg = new String('a', 14);
Thanks.
CrishPosted May 5, 2010, 6:30 AM
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
Jaish MathewsPosted May 5, 2010, 4:14 AM
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;
}
}
Sagar KumarPosted May 5, 2010, 3:49 AM
if lenth >150 then only do it rest of the thing is same.
but in else send the message and break the whilw loop.