Home > Software engineering >  Trim Text Between Two Similar Identifiers C#
Trim Text Between Two Similar Identifiers C#

Time:03-29

I have a string

string myString = "NotNeeded/ThisTextIsNeeded/NotNeeded";

I just need "ThisTextIsNeeded" and the starting and ending identifiers are same "/". I can use Substring but for that I will need different identifiers. Can anyone please guide me how to do it using similar identifiers in C#.

Edit: The string cannot be necessarily "NotNeeded/ThisTextIsNeeded/NotNeeded". It can also be changed for example "ABC/ThisTextIsNeeded/ABC" or "AAA/APPPP/ABC". I just need any text that comes in between / and /.

Thanks

CodePudding user response:

1- You can use regex

string regexPattern = @"(?<=\/)(.*?)(?=\/)";
var match = Regex.Match("NotNeeded/ThisTextIsNeeded/NotNeeded", regexPattern);

Output: ThisTextIsNeeded

2- Or, Split string by using "/"

string text = "NotNeeded/ThisTextIsNeeded/NotNeeded";
string[] list = text.Split('/');

Output: list[1] will be -> ThisTextIsNeeded

  • Related