|
You last visited: Today at 05:24
Advertisement
C# Find string between two stings !
Discussion on C# Find string between two stings ! within the .NET Languages forum part of the Coders Den category.
07/10/2012, 03:30
|
#1
|
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
|
C# Find string between two stings !
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
|
#2
|
elite*gold: 0
Join Date: May 2010
Posts: 2,945
Received Thanks: 255
|
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
|
#3
|
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
|
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
|
|
|
07/10/2012, 10:44
|
#4
|
elite*gold: 966
Join Date: Apr 2010
Posts: 1,105
Received Thanks: 681
|
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 
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
|
#5
|
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
|
Away from the very nice sense of humor that you have... thx
|
|
|
07/10/2012, 17:10
|
#6
|
elite*gold: 0
Join Date: Jul 2012
Posts: 67
Received Thanks: 23
|
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
|
#7
|
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
|
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
|
#8
|
elite*gold: 0
Join Date: May 2008
Posts: 477
Received Thanks: 178
|
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
|
elite*gold: 0
Join Date: May 2011
Posts: 500
Received Thanks: 261
|
u can use regex to find data between 2 strings
|
|
|
07/13/2012, 16:10
|
#10
|
elite*gold: 0
Join Date: Sep 2005
Posts: 427
Received Thanks: 87
|
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
|
#11
|
elite*gold: 12
Join Date: Aug 2011
Posts: 455
Received Thanks: 418
|
Made by CAS!
PHP Code:
private static string[] StringBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool 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(0, iEnd);
if (iEnd + strEnd.Length < strSource.Length) result[1] = strSource.Substring(iEnd + strEnd.Length);
}
}
else
result[1] = strSource;
return result;
}
|
|
|
 |
Similar Threads
|
[Visual Basic] [Problem] String auslesen/String zufällig wählen
05/06/2012 - General Coding - 4 Replies
Code:
#include <GUIConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Name Generator", 236, 299, 815, 246)
$Input1 = GUICtrlCreateInput("Username", 24, 72, 185, 21)
$Input2 = GUICtrlCreateInput("Username", 24, 104, 185, 21)
$Input3 = GUICtrlCreateInput("Username", 24, 136, 185, 21)
$Input4 = GUICtrlCreateInput("Username", 24, 168, 185, 21)
$Input5 = GUICtrlCreateInput("Username", 24, 200, 185, 21)
|
[VB08]String in String mit mehreren Funden
08/08/2011 - .NET Languages - 6 Replies
Hey,
bin gerade auf ein Problem gestoßen, an dem ich mir seit 3 Stunden die Zähne ausbeiße.
Ich will eine Funktion schreiben, die der _StringBetween Funktion von AutoIt gleich ist. _StringBetween gibt in einem Array alle Strings zwischen zwei SubStrings und dem ganzen String aus. Die Ausgabe bei
_StringBetween("<h1>test1</h1>&l t;h1>test2</h1>", "<h1>", "</h1>") wäre also idealer Weiße ein Array (x = "test1", x = "test2")...
da man in VB08 kein Array returnen kann, komme ich aber einfach...
|
[C++] string zwischen string
11/11/2010 - C/C++ - 6 Replies
tag gibts direkt ne funktion, mit der man einen passenden string zwischen dem string suchen kann?
also meine net .find() sondern sowas ähnliches, die in diesem beispiel "mein string sucht"
Bsp:
"<span id=\"lalala\">"+string mein_string+"</span>"
understanden? :-)
|
How to... lvl a bard on stings with openkore
06/21/2006 - Ragnarok Online - 5 Replies
Hello guys.
I tried to use openkore on the gh-stings map, but my problems are the following:
when i use this one:
runFromTarget 1
runFromTarget_dist 6
he runs to far away, and mostly directly into the next sting so that he has to teleport.
|
All times are GMT +1. The time now is 05:25.
|
|