user1290653
user1290653

Reputation: 775

Pull string which is surrounded by "/" from a string C#

I would like to take the words of a sentence, if that word has in each end, the forward slash character /

For example, a string should contain spaces so may have to pull a string from something like:

Example:

"Hello /World/" --> "World"

Would I have to use regular expressions for this, if so, could you tell me how to do this please? If not, how could I do it in a really basic way?

Upvotes: 0

Views: 928

Answers (2)

L.B
L.B

Reputation: 116178

Using Regex,

string[] results = Regex.Matches("Hello /World/ hello /universe/",@"/(.+?)/")
    .Cast<Match>()
    .Select(m=>m.Groups[1].Value)
    .ToArray();

would return World and universe

Upvotes: 3

iefpw
iefpw

Reputation: 7062

Loop through all the characters. Get the first index of / on the position find the paired last / on the position and remove anything between by creating a new string. Keep looping and find the opening and closing / until finish.

Get the first index of /, last index of / and split and string.substring() it.

Upvotes: 2

Related Questions