StringBetween in C#

01/04/2015 17:26 miejscov#1
If you was using AutoIT you probably know function named StringBetween. When I started to write in C# I was missing it, so i have wrote new one:
Code:
public static string[] StringBetween(string code, string before, string after)
        {
            bool exist = true;
            string[] result = new string[0];
            int indexxx = 0;
            while (exist)
            {
                try
                {
                    int beforeindex = code.IndexOf(before) + before.Length;
                    int afterindex = code.IndexOf(after, beforeindex);
                    int test = code.IndexOf(before);
                    if (code.IndexOf(before) != -1 && code.IndexOf(after) != -1)
                    {
                        Array.Resize(ref result, result.Length + 1);
                        result[indexxx] = code.Substring(beforeindex, afterindex - code.IndexOf(before) - before.Length);
                        code = code.Remove(0, afterindex + after.Length);
                        indexxx += 1;
                    }
                    else
                    { exist = false; }
                }
                catch { exist = false; }
            }
            return result;
        }
01/04/2015 18:13 alpines#2
It would be somehow more enlightening if you could translate this function into english.
Next time post it here: [Only registered and activated users can see links. Click Here To Register...]
01/04/2015 18:51 Ludder231#3
PHP Code:
        private string GetBetween(string strSourcestring strStartstring strEnd)
        {
            
int iPos 0;
            
int iEnd 0;
            
int lenStart strStart.Length;
            
string strResult null;

            
strResult string.Empty;
            
iPos strSource.IndexOf(strStart0);
            
iEnd strSource.IndexOf(strEndiPos lenStart);
            if (
iPos != -&& iEnd != -1)
            {
                
strResult strSource.Substring(iPos lenStartiEnd - (iPos lenStart));
            }
            return 
strResult;
        } 
is a good solution too
01/04/2015 19:02 snow#4
#moved
01/04/2015 19:41 alpines#5
Quote:
Originally Posted by Ludder231 View Post
PHP Code:
        private string GetBetween(string strSourcestring strStartstring strEnd)
        {
            
int iPos 0;
            
int iEnd 0;
            
int lenStart strStart.Length;
            
string strResult null;

            
strResult string.Empty;
            
iPos strSource.IndexOf(strStart0);
            
iEnd strSource.IndexOf(strEndiPos lenStart);
            if (
iPos != -&& iEnd != -1)
            {
                
strResult strSource.Substring(iPos lenStartiEnd - (iPos lenStart));
            }
            return 
strResult;
        } 
is a good solution too
This one returns no array with the found matches.
01/04/2015 20:11 miejscov#6
Sorry, I changed it to English in first post
02/11/2015 19:53 warfley#7
This snippet might work well in some cases, but there are tons of problems coming with this snippet.
For example the String: "(A(B)C)" you can Interpret StringBetwen with ( and ) as two ways:
1. The result could be (most obvious one) An array with
"A(B"
"B"
2. The (mathematical-)Logical one:
"A(B)C"
"B"
3. The complete one:´
"A(B"
"A(B)C"
"B"
"B)C"


Just a matter how you define what kind you want. Your Function but only gives a 1 elemented Array with the element "A(B". Thats just wrong.


And btw there are some other minor issues, that could be done better.
1. Dont use an Array that way, use a List instead, thats what they are made for.
2. You dont need the variable indexxx, dont use it
3. You dont need the variable test, dont use it.
4.
Code:
                    int beforeindex = code.IndexOf(before) + before.Length;
                    int afterindex = code.IndexOf(after, beforeindex);
                    if (code.IndexOf(before) != -1 && code.IndexOf(after) != -1)
In this part you save the index in a variable and then in the if statement you just use IndexOf again, which searches the whole text again (what can be on large strings time intensive)
5. Don't use try-catch in that way its not the task of the method to handle exceptions, the calling function which gives the string needs to do the handling.


The minor issues are not really a problem, but you should fix the big problem

A Pseudo solution:
Code:
Method StringBetwen(Str, start, end, List)
  StartIndices: List
  EndIndices: List
  For i:=0 to StrLength-EndLength-1
    If Str(i) = Start then
      StartIndices.Add(i)
    If Str(i) = End Then
      EndIndices.Add(i)
  For i_s in StartIndices
    For i_e in EndIndices
      if i_e > i_s then
        List.Add(Copy(str, I_s, I_e))
01/08/2016 21:55 ĤΛƇҠ#8
Extended Edition:

Code:
/// <summary>
        /// Gets the String inbetween two strings in a string
        /// </summary>
        /// <param name="strBegin">Start of searched string</param>
        /// <param name="strEnd">End of searched string</param>
        /// <param name="strSource">SSource string</param>
        /// <param name="includeBegin">Should the begin be included in the result?</param>
        /// <param name="includeEnd">Should the end be included in the string?</param>
        /// <returns>The String inbetween.</returns>
        public static string[] GetStringInBetween(string strBegin,
    string strEnd, string strSource,
    bool includeBegin, bool includeEnd, bool OnEOFReturn = false)
        {
            string[] result = { "", "" };
            int iIndexOfBegin = strSource.IndexOf(strBegin);
            if (iIndexOfBegin != -1)
            {
                // include the Begin string if desired
                if (includeBegin)
                    iIndexOfBegin -= strBegin.Length;
                strSource = strSource.Substring(iIndexOfBegin
                    + strBegin.Length);
                int iEnd = strSource.IndexOf(strEnd);
                if (iEnd != -1)
                {
                    // include the End string if desired
                    if (includeEnd)
                        iEnd += strEnd.Length;
                    result[0] = strSource.Substring(0, iEnd);
                    // advance beyond this segment
                    if (iEnd + strEnd.Length < strSource.Length)
                        result[1] = strSource.Substring(iEnd
                            + strEnd.Length);
                }
                else
                {
                    if (OnEOFReturn)
                        return new string[] { strSource };
                }
            }
            else
                // stay where we are
                result[1] = strSource;
            return result;
        }
01/20/2016 17:35 Freshek#9
Code:
public static string StringBetween(string str, string start, string end)
        {
           return Regex.Match(str, start + "(.+?)" + end).Groups[1].Value;
        }
01/20/2016 21:54 alpines#10
Quote:
Originally Posted by Freshek View Post
Code:
public static string StringBetween(string str, string start, string end)
        {
           return Regex.Match(str, start + "(.+?)" + end).Groups[1].Value;
        }
As I said earlier, this solution does not return the collection of found strings. Usually StringBetween (taken from AutoIt) returns an array containing the different strings.
01/21/2016 18:37 YatoDev#11
Quote:
Originally Posted by alpines View Post
As I said earlier, this solution does not return the collection of found strings. Usually StringBetween (taken from AutoIt) returns an array containing the different strings.
then use Regex.Matches and iterate the MatchCollection