How to get particular text from string in C#
.net String.Substring method allow us to retrieve a substring from a string object. this method have two overloaded method those are Substring(Int32) and Substring(Int32, Int32).
Substring(Int32) overloaded method retrieve a substring from a string by starting at a specified character position and continues to the end of the string. this method need to pass a parameter name startIndex. this parameter data type is System.Int32. startIndex parameter value specify a zero based starting character position of the string. this method does not modify the current string instance, it returns a new string that begins at the startIndex position of the current string.
Substring(Int32, Int32) overloaded method allow us to get a substring from a string that start a specified character position of string and has a specified length. this method have two parameters, those are startIndex and length. startIndex is the zero based starting character position of a substring in this instance. length parameter specify the number of characters in the substring.
the folLowing console c# example code demonstrate us how can we get a substring from a string in .net framework.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Program { static void Main(string[] args) { string myString = "Hello World, Lorem Ipsum"; string subString = myString.Substring(0, 5); Console.WriteLine("Substring[0,5]: " + subString); Console.ReadLine(); } } |
Output:
1 2 3 | Substring[0,5]: Hello |