C# .NET - SubString in C#

Asked By A S on 06-Dec-05 12:04 PM
Question: How to retrieve substring from a string in C# to test “\n” in the last of the string?

Look at the Substring static method

Asked By Peter Bromberg on 06-Dec-05 12:13 PM
of the String class.
you can also use the IndexOf and LastIndexOf methods:
if(mystring.IndexOf("\n") >-1)
{
 // yup, there's a "\n" in there!
}

Not quite sure what your

Asked By Jon Wojtowicz on 06-Dec-05 12:46 PM
trying to do. To find obtain a substring you use the Substring method of the string.
string myString = "test1\ntest2\ntest3";
string test = myString.Substring(0, myString.IndexOf("\n")); // test = "test1"
test = myString.Substring(myString.LastIndexOf("\n") + 1);  // test = "test3"
test = myString.Substring(myString.IndexOf("\n") + 1, myString.LastIndexOf("\n")- myString.IndexOf("\n")); // test = "test2"

String EndsWith

Asked By F Cali on 06-Dec-05 02:34 PM
Hi Anil,
You can use the EndsWith method of the String object to determine if the last character is "\n" and perform whatever you want on the string:
string myString = "This is a Test String\n";
if (myString.EndsWith("\n"))
myString = myString.Substring(0, myString.Length - 1);
Hope this helps.