Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find text in string with C#

Tags:

string

c#

find

How can I find given text within a string? After that, I'd like to create a new string between that and something else. For instance, if the string was:

This is an example string and my data is here 

And I want to create a string with whatever is between "my " and " is" how could I do that? This is pretty pseudo, but hopefully it makes sense.

like image 605
Wilson Avatar asked May 22 '12 20:05

Wilson


People also ask

How do I find a word in a string in C?

Search for a character in a string - strchr & strrchr The strchr function returns the first occurrence of a character within a string. The strrchr returns the last occurrence of a character within a string. They return a character pointer to the character found, or NULL pointer if the character is not found.

What is strstr () in C?

The strstr() function returns pointer to the first occurrence of the matched string in the given string. It is used to return substring from first match till the last character.

How do you check if a string contains a letter in C?

C isalpha() The isalpha() function checks whether a character is an alphabet or not. In C programming, isalpha() function checks whether a character is an alphabet (a to z and A-Z) or not. If a character passed to isalpha() is an alphabet, it returns a non-zero integer, if not it returns 0.

Is substring in C?

Implement substr() function in CThe substr() function returns the substring of a given string between two given indices. It returns the substring of the source string starting at the position m and ending at position n-1 .


1 Answers

Use this method:

public static string getBetween(string strSource, string strStart, string strEnd) {     if (strSource.Contains(strStart) && strSource.Contains(strEnd))     {         int Start, End;         Start = strSource.IndexOf(strStart, 0) + strStart.Length;         End = strSource.IndexOf(strEnd, Start);         return strSource.Substring(Start, End - Start);     }      return ""; } 

How to use it:

string source = "This is an example string and my data is here"; string data = getBetween(source, "my", "is"); 
like image 138
Oscar Jara Avatar answered Oct 14 '22 14:10

Oscar Jara