How do you use .Substring?

C#:
            string strOld = "The dog eat my homework";
            string subStr = strOld.Substring(12, 11);
            string srtNew = string.Concat("I did ", subStr);
strOld equals The dog eat my homework
By calling .Substring() on strOld, and specifying the start index, and the endpoint index which is the length of the index position from where your start index is, it is kinda like cutting a whole in the string and taking only the characters in-between the indexes specified, which gives you partial character elements of the original string.

When we use .Substring(12, 11); subStr becomes my homework. (Although, you don't have to specify the last endpoint index, if you don't want too. You could just do strOld.Substring(12);) Then srtNew = string.Concat("I did ", subStr); becomes I did my homework. string.Concat only joins the two strings "I did" and subStr together to complete the full sentence.

I think you would be better reading the documentation from MSDN : String.Substring Method (System), and I also think you should be searching these question on the interwebs before posting them in a forum. As they have been answered a bunch of times before, and only if you still don't understand how it works, should you be turning to forums for help.
 
Considering our OP's previous thread about Int32.TryParse(), and this thread's query about String.Substring() right after each other, it feels like the OP is trying to get us to do his homework for him. I feel that these are homework questions designed to make the student learn how to search, read, and understand the MSDN documentation.
 
I did see that and it's what caused me to post to make it obvious. :)
 
Back
Top Bottom