C# Find string between two stings !

07/10/2012 03:30 badguy4you#1
now i have a richtextbox1 that has alot of text

what i want to achieve is search for as example "First" and "Second" in the textbox then get the strings located between them

i tried to use this but its hell slow

while((a9 = richtextbox1.Text) != null)
{
if ( line.Contains("word") )
{
string theline = line;
}

counter++;
}
07/10/2012 03:55 Vloxxity#2
versuchs mal so in etwa ...

Code:
string teststring = "ich bin First und du bist Second okay";
string newString = teststring.Substring(teststring.IndexOf("First"), teststring.IndexOf("Second"));
07/10/2012 04:26 badguy4you#3
i dont need a specified text string i need it to search the whole richbox for these two words and get text located between them

But anywat thanks for your help and if you know how could i achieve that please tell me :D
07/10/2012 10:44 Jeoni#4
What Vloxxity said was good. If you have a RichTextBox you can do it like that:
Code:
string teststring = RichTextBox1.Text; //Should be clear 
string newString = teststring.Substring(teststring.IndexOf("First"), teststring.IndexOf("Second"));
// Or a 1-line Code:
string newString = RichTextBox1.Text.Substring(RichTextBox1.Text.IndexOf("First"), RichTextBox1.Text.IndexOf("Second"));
Credits to Vloxxity :D
Is that what you are searching for? The methode Vloxxity posted was enough to archieve that. And I don't think you are a C&P-Kiddie, who can't apply general methods to specific problems (then you should stop programmming immediately).
Best regards
Jeoni
07/10/2012 17:09 badguy4you#5
Away from the very nice sense of humor that you have... thx
07/10/2012 17:10 Miszczu.#6
Code:
string between(string str1, string str2, string str3) 
{
 int start = str1.IndexOf(str2);
 int end = str1.IndexOf(str3, start);
 return str1.Substring(start + str2.Length, end - start - str2.Length);
}
07/10/2012 21:46 badguy4you#7
and your code does not work at all cuz the rich box i have has more that 400 line full of text so how i do that
07/13/2012 03:37 badguy4you#8
bump

the richbox I have has more than 500 lines I want to search for the unique text "FIRST" and the other unique text "Second" and get the text between then

EX. a line container this "First time I code in C# and it is my Second time"

of meaningless sentence cuz I don't have example now but I want to get that text "time I code in C# and it is my"

NOTE: The rich box has more than 500 lines I want the fastest way to get the results desired
07/13/2012 13:29 ßΙЍȺƦƴßȰȾ#9
u can use regex to find data between 2 strings
07/13/2012 16:10 kissein#10
u can build your own custom method

First define your SplitDefinition

Code:
public class SplitDefinition
{
    public SplitDefinition(char delimiter, string[] splits)
    {
        this.delimiter = delimiter;
        this.splits = splits;
    }

    public char delimiter { get; set; }
    public string[] splits { get; set; }
}
then use List to store your results

Code:
public List<SplitDefinition> CustomSplit(string source, char[] delimiters)
{
    List<SplitDefinition> splitDefinitionList = new List<SplitDefinition>();

    foreach(char d in delimiters)
    {
        SplitDefinition sd = new SplitDefinition(d, source.Split(d));           
        splitDefinitionList.Add(sd);
    }

    return splitDefinitionList;
}
and init it
Code:
void Main()
{
    string text = "First time I code in C# and it is my Second time";
    List<SplitDefinition> splitDefinitionList = CustomSplit(text, new char[] { 'First', 'Second' });
}
07/26/2012 02:20 Naworia#11
Made by CAS!

PHP Code:
private static string[] StringBetween(string strBeginstring strEndstring strSourcebool includeBeginbool includeEnd)
        {

            
string[] result = { """" };
            
int iIndexOfBegin strSource.IndexOf(strBegin);
            if (
iIndexOfBegin != -1)
            {
                if (
includeBegin)
                    
iIndexOfBegin -= strBegin.Length;
                
strSource strSource.Substring(iIndexOfBegin strBegin.Length);
                
int iEnd strSource.IndexOf(strEnd);
                if (
iEnd != -1)
                {
                    if (
includeEnd)
                        
iEnd += strEnd.Length;
                    
result[0] = strSource.Substring(0iEnd);
                    if (
iEnd strEnd.Length strSource.Lengthresult[1] = strSource.Substring(iEnd strEnd.Length);
                }
            }
            else
                
result[1] = strSource;
            return 
result;
        }