2

I have a message which is say 287 characters long. I need to split it in two after 160 chars, but my code continues to not work. I've googled so much and tried so many different solutions, but nothing is working as I would expect. In my head, this is a simple solution, yet in practice it's causing me nightmares!

// a check is done to ensure the message is > 160 in length.    
string _message;
_message = "this is my long message which needs to be split in to two string after 160 characters. This is a long message. This is a long message. This is a long message. This is a long message. This is a long message.";

string message1 = _message.Substring(0,160);
string message2 = _message.Substring(161,_message.Length);

The above simply doesn't work though - giving me an exception error on the second substring.

Can anyone help? The message will never be more than 320 characters.

6 Answers 6

7

String.Substring does start at the first parameter and has a length of the second parameter. You have passed message.Length as second parameter, that doesn't work.

You can use the overload with just one parameter(from start to end):

string firstPart = _message.Substring(0,160);
string rest = _message.Substring(160);

Throws an ArgumentOutOfRangeException if the startIndex is less than zero or greater than the length of the string.

demo: http://ideone.com/ZN2BlM

Sign up to request clarification or add additional context in comments.

1 Comment

-1 when you taked first 160 items and you want take next items, your start index is 160 not 161.
6

For the second line just use

string message2 = _message.Substring(160);

If your string could be less than 160 characters, you should check for that.

2 Comments

@SaeedAmiri Quite right. I see you made the same error yourself :)
I made a same mistake at first, but I fixed it then I told you and others
4
string message1 = _message.Substring(0,160);
string message2 = _message.Substring(160,_message.Length - 160);

See This for using two argument substring.

Comments

1

There is an overload of the String.Substring function that does not take the lenght parameter but just go to the end of the string. You could simplify your code in this way:

string message1 = _message.Substring(0,160);
string message2 = _message.Substring(160);

Comments

1

The second parameter to the Substring method receive the number or chars you want to take from _message. Instead do this:

string message1 = _message.Substring(0,160);
string message2 = _message.Substring(160,_message.Length-160);

Substring method in C#

2 Comments

Anyway I didn't downvote you. I just downvoted two available answers at the time I wrote the comment, to notify them to fix their mistake to do not confuse OP by wrong answers, and I removed the downvotes after they corrected their answer.
mmm... I guess that I'm not lucky
0

According to http://msdn.microsoft.com/en-us/library/aa904308(v=vs.71).aspx the function has the following footprint : substring (int start) or substring(int start, int length)

Meaning the way you're calling it says : Start copying from position 160, and continue for the total length of the string. So if your string is 287 characters long, then you're telling it by using

string message2 = _message.Substring(161,_message.Length);

Start copying at position 161, and continue for the following 287 characters. In that case the string would have to be 161 + 287 characters, which is the cause of your error.

You should be using :

string _message;
_message = "this is my long message which needs to be split in to two string after 160 characters. This is a long message. This is a long message. This is a long message. This is a long message. This is a long message."; string message1 = _message.Substring(0,160);
string message2 = _message.Substring(message1.Length, _message.Length - message1.Length);

Which will result in a message having a length of 287 - 160 = 127 .

2 Comments

When I downvote someone, usually I'm writing my reason (except very clear case), for your answer, no I didn't downvote you, but in any case it's not good to ask someone, to see if is a downvoter or not, IMHO it's rude.
@SaeedAmiri Apologies then, but I didn't see anyone else Having done a downvote . No insult meant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.