Home > Blockchain >  Check if a string contains a word?
Check if a string contains a word?

Time:04-13

I need to check if a url starts a certain way. So the proper way to do this would be

url.StartsWith("word1/{randomWord}/word2").

Is there a way for this to pass with random Word being any number of letters or numbers?

Edit: Any number of letters or numbers*

CodePudding user response:

You could use a Regex:

regex.IsMatch(url, @"^word1/[A-Za-z0-9] /word2")
  • The "^" indicates that this must be at the start of the url
  • The "[A-Za-z0-9]" specifies the characters that may be in the random word (adapt as required).
  • The " " indicates that the length of the random word must be at least 1.

CodePudding user response:

You need string interpolation, so like: $"word1/{randomWord}/word2 In this way randomWord can be a string with any value

  • Related