We will learn about how to get words or character inside of parentheses in C#
IndexOf()
gives you the first position of a character or string inside of another string.
IndexOf()
returns -1 if it can't find a match.
Substring()
returns just the specified portion of a string, using a starting position and optional length.
Example:
string message = "Find what is (inside the parentheses)";
int openingPosition = message.IndexOf('(');
int closingPosition = message.IndexOf(')');
openingPosition += 1;
int length = closingPosition - openingPosition;
Console.WriteLine(message.Substring(openingPosition, length));
That code will return output like this:
inside the parentheses
We can use the
IndexOfAny()
method to find the first location of any of the string from selected array.And we can also use
LastIndexOf()
to find the final location of a string within another string. Source: https://learn.microsoft.com/id-id/training/modules/csharp-modify-content/2-exercise-indexof-substring