Hilfe beim splitten von Strings.
Hallo Community, ich bitte euch noch ein mal um Hilfe. Ich versuche mich an C# for Windows Phone und versuche gerade einen Text zu splitten. Also z.B. die Eingabe eines Users: "Hallo + Wie + Geht + Es + Dir" in "Hallo wie geht es dir".
Code:
[FONT=Consolas][FONT=Consolas][FONT=Consolas]private[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]void[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas] Button_Click_1([/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]object[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas] sender, [/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]RoutedEventArgs[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas] e)[/FONT][/FONT]
[FONT=Consolas][FONT=Consolas]
{
[/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]string[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas] userEingabe;[/FONT][/FONT]
[FONT=Consolas][FONT=Consolas]
userEingabe = myTextBox1.Text;
[/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]char[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas][] delimiterChars = { [/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]' '[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas],[/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]'+'[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas]};[/FONT][/FONT]
[FONT=Consolas][FONT=Consolas]
[/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]string[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas] text = userEingabe;[/FONT][/FONT]
[FONT=Consolas][FONT=Consolas]
[/FONT][/FONT][FONT=Consolas][FONT=Consolas] myTextBlock1.Text = text; [/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]//ausgabe des Originaltextes[/FONT][/FONT][/FONT]
[FONT=Consolas][FONT=Consolas][FONT=Consolas]
[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas]
[/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]string[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas][] words = text.Split(delimiterChars);[/FONT][/FONT]
[FONT=Consolas][FONT=Consolas]
[/FONT][/FONT][FONT=Consolas][FONT=Consolas] myTextBlock2.Text = words.Length.ToString(); [/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]//ausgabe der anzahl der Wörter[/FONT][/FONT][/FONT]
[FONT=Consolas][FONT=Consolas][FONT=Consolas]
[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas]
[/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]foreach[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas] ([/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]string[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas] s [/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]in[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas] words)[/FONT][/FONT]
[FONT=Consolas][FONT=Consolas]
{
[/FONT][/FONT][FONT=Consolas][FONT=Consolas] myTextBlock3.Text = s; [/FONT][/FONT][FONT=Consolas][FONT=Consolas][FONT=Consolas]// alle einzelnen wörter??????[/FONT][/FONT][/FONT]
[FONT=Consolas][FONT=Consolas][FONT=Consolas]
[/FONT][/FONT][/FONT][FONT=Consolas][FONT=Consolas] }
[/FONT][/FONT]
vielleicht hilft euch das weiter bei meinem Problem. Wie kann ich jetzt nur die Worte wieder ausgeben? Eventuell auch Zahlen davon trennen? Und wenn wir so weit kommen, die Wörter in Strings Speichern und Die Zahlen weiter nutzen in Ints? Wäre super wenn ihr mir dabei behilflich sein könnt:) ich hoffe ihr versteht mein Problem. danke im Vorraus Agredo
AW: Hilfe beim splitten von Strings.
du hast die Wörte in einen array gespeichert. also musst du diesen wieder zusammenfügen.
Code:
string strFiller;
foreach (string s in words)
{
strFiller += s; // fügt jedes mal s zu strFiller hinzu.
strFiller = strFiller+s; // geht genau so, ist nur nicht so hübsch ;)
}
myTextBlock3.Text = strFiller; //und nun ausgeben
AW: Hilfe beim splitten von Strings.
danke dür die schnelle Antwort aber er gibt mir dann als Fehlermeldung: Verwendung der nicht zugewiesenen lokalen Variablen "strFiller" also in der Zeile: strFiller += s;
AW: Hilfe beim splitten von Strings.
Zitat:
Zitat von
yjeanrenaud
Code:
string strFiller = "";
foreach (string s in words)
{
strFiller += s; // fügt jedes mal s zu strFiller hinzu.
strFiller = strFiller+s; // geht genau so, ist nur nicht so hübsch ;)
}
myTextBlock3.Text = strFiller; //und nun ausgeben
Kleine Korrektur - bei der Deklaration wird jetzt strFiller mit einem Leerstring initialisiert, dann sollte es gehen.
AW: Hilfe beim splitten von Strings.
jaa, danke:) jetzt klappt das ja schon mal FAST:) jetzt gibt er mir, wenn ich zum Beispiel "Hallo+wie+geht+es+dir" schreibe. Hallowiegehtesdir.
wenn ich es richtig verstanden habe, habe ich doch die einzelnen Wörter in Arrays gespeichert, oder? also kann ich diese aus word [0] als einzelnes wort ausgben? Geht dieses dann auch aus einem mix aus Zahlen und Wörtern? zB. hallo3+wie2+geht1+es4+dir5 in hallo wie geht es dir und 3 2 1 4 5 ?
AW: Hilfe beim splitten von Strings.
AW: Hilfe beim splitten von Strings.
Na ja, bei nem int[] Array muss er dann halt erst noch ToString() aufrufen, sonst wird das Plus nämlich die Zahlen addieren statt sie hintereinanderzuhängen. Hast du jedoch die Zahlen in einem String[] Array geht es so wie hier beschrieben.
arr[0].ToString() wandelt dir wenn arr ein Int-Array ist den Wert an der Stelle 0 zu einem String um
Int32.Parse(arr[0]) wandelt dir wenn arr ein String-Array ist den String an der Stelle 0 zu einem Int um (vorrausgesetzt da steht eine Zahl drinnen in einem Format, dass geparst werden kann - wenn der String jetzt aber "Hallo" ist dann gibts ne Exception).