Huma Ali Huma Ali - 17 days ago 5
C# Question

Remove a substring from String

Is there a way I can remove substrings from my string on the basis of any identifier?
Like I have

string codeID = "347439>Dome";


I want to have only that part of string that occurs before
>

So it becomes:

string newCodeId = "347439"


Is it possible?

Answer

this could be another possibility:

string codeID = "347439>Dome";

string newCodeId = codeID.Split('>')[0];

Console.WriteLine(newCodeId);

It uses the String.Split() method which returns a string[]. To get the left part of the string here I took the first element in the array. The rest of the string is (in this case) in position 1.

in the case that there is not > separator in the string. it will just the entire old string in position [0]

Comments