Register for your free account! | Forgot your password?

You last visited: Today at 18:27

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



StringBetween in C#

Discussion on StringBetween in C# within the Coding Snippets forum part of the Coding Releases category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Jan 2015
Posts: 2
Received Thanks: 0
StringBetween in C#

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;
        }
miejscov is offline  
Old 01/04/2015, 18:13   #2
 
alpines's Avatar
 
elite*gold: 60
Join Date: Aug 2009
Posts: 2,256
Received Thanks: 815
It would be somehow more enlightening if you could translate this function into english.
Next time post it here:
alpines is offline  
Old 01/04/2015, 18:51   #3
 
Ludder231's Avatar
 
elite*gold: 0
Join Date: Aug 2010
Posts: 880
Received Thanks: 113
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
Ludder231 is offline  
Old 01/04/2015, 19:02   #4

 
snow's Avatar
 
elite*gold: 724
Join Date: Mar 2011
Posts: 10,479
Received Thanks: 3,318
Arrow .NET Languages -> Coding Snippets

#moved
snow is offline  
Old 01/04/2015, 19:41   #5
 
alpines's Avatar
 
elite*gold: 60
Join Date: Aug 2009
Posts: 2,256
Received Thanks: 815
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.
alpines is offline  
Old 01/04/2015, 20:11   #6
 
elite*gold: 0
Join Date: Jan 2015
Posts: 2
Received Thanks: 0
Sorry, I changed it to English in first post
miejscov is offline  
Old 02/11/2015, 19:53   #7
 
elite*gold: 0
Join Date: Feb 2009
Posts: 1,137
Received Thanks: 573
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))
warfley is offline  
Old 01/08/2016, 21:55   #8
 
ĤΛƇҠ's Avatar
 
elite*gold: 40
Join Date: Mar 2011
Posts: 771
Received Thanks: 352
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;
        }
ĤΛƇҠ is offline  
Old 01/20/2016, 17:35   #9
 
Freshek's Avatar
 
elite*gold: 0
Join Date: Aug 2015
Posts: 803
Received Thanks: 1,359
Code:
public static string StringBetween(string str, string start, string end)
        {
           return Regex.Match(str, start + "(.+?)" + end).Groups[1].Value;
        }
Freshek is offline  
Old 01/20/2016, 21:54   #10
 
alpines's Avatar
 
elite*gold: 60
Join Date: Aug 2009
Posts: 2,256
Received Thanks: 815
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.
alpines is offline  
Old 01/21/2016, 18:37   #11
 
YatoDev's Avatar
 
elite*gold: 50
Join Date: Sep 2012
Posts: 3,841
Received Thanks: 1,462
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
YatoDev is offline  
Reply


Similar Threads Similar Threads
Hilfe StringBetween
09/09/2014 - AutoIt - 5 Replies
Hallo, ist wer so nett :cool: und könnte mir bitte bei StringBetween Helfen ? und zwar möchte ich aus nachfolgender Zeile alle einzelnen Werte in einer Tabelle abrufen können oder angezeigt bekommen mit _ArrayDisplay. so sieht die Zeile aus: daten=260540193;1;4494;affenspiele.xy;0;0;30;100;0 ;0;0;0;0;0;1;1;Emilio;ok;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;; Das es dann so ausschaut: 0 | 260540193 1 | 1
[PHP]stringBetween Funktion
02/18/2014 - Coding Snippets - 18 Replies
Ich brauchte eine Funktion, die in einem String zwischen 2 angegeben Zeichenfolgen, den Text rausbekommt. Da ich RegEx nicht so mag, habe ich kurz eine Funktion dafür geschrieben: function stringBetween($content, $strStart, $strEnd) { $lenStart = strlen($strStart); $strResult = ""; $iPos = strpos($content, $strStart, 0); $iEnd = strpos($content, $strEnd, $iPos + $lenStart); if($iPos != false && $iEnd != false) {
Winhttp Stringbetween
10/23/2013 - AutoIt - 3 Replies
Hey, ich möchte vom quelltext die value rausnehmen. Jedoch gibt es mehrere values diese auch unterschiedlich sind. Darum brauche ich den teil vor value = auch aber in diesen teil sind " drinnen. Und wenn ich das einfüge in Autoit schließt sich das ja wieder weil da dann zwei " sind
StringBetween --- Problem-Need Help ^^
11/14/2010 - AutoIt - 20 Replies
Wie der Titel schon sagt geht es um _StringBetween(denke ich) Hier der Code: Func _firefoxrid() _FFStart(GUICtrlRead($input3)) WinSetState ( "MozillaFirefox", "", @SW_MINIMIZE ) $sHTML = _FFReadHTML("html",16) $rid = _StringBetween($sHTML, "var rid = '", "';") _FFWindowSelect( "", "label") Local $time1= _StringBetween($sHTML, 'new product('&GUICtrlRead($Input2), 'nbsp') ;$time2= StringRight($time1, 8)
Auch ein StringBetween Problem..
11/08/2010 - AutoIt - 16 Replies
also habe untenstehenden code... und es kommt immer der fehler: Subscript used with non array varriable. woran könnte es liegen^^ die youtube seite ist eig geöffnet und bei nem andren code hat genau das gefunzt.. alle includes sind eig gemacht... weis es net^^ sagt wenn ihr mehr infos braucht^^



All times are GMT +1. The time now is 18:27.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.