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))
/// <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;
}
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.
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
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^^