Matt Facer
Matt Facer

Reputation: 3105

Split a string using substring out of range

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.

Upvotes: 2

Views: 6667

Answers (6)

Tomas Ramirez Sarduy
Tomas Ramirez Sarduy

Reputation: 17471

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#

Upvotes: 1

Daneo
Daneo

Reputation: 518

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 .

Upvotes: 0

il_guru
il_guru

Reputation: 8508

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);

Upvotes: 1

podiluska
podiluska

Reputation: 51504

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.

Upvotes: 6

Tim Schmelter
Tim Schmelter

Reputation: 460208

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

Upvotes: 7

Saeed Amiri
Saeed Amiri

Reputation: 22565

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

See This for using two argument substring.

Upvotes: 4

Related Questions