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.
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.
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.
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.
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 .
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With