Autor Beitrag
DareDevil
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Mi 18.04.07 15:56 
So habe mal wieder eine Frage,

ist es möglich das man einen String splittet und dann aber noch die Splitter auch ausgegeben werden.

Greez

Pascal
korken
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Mi 18.04.07 16:09 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
char[] delimiterChars = { ' '',''.'':''\t' };

        string text = "one\ttwo three:four,five six seven";
        System.Console.WriteLine("Original text: '{0}'", text);

        string[] words = text.Split(delimiterChars);
        System.Console.WriteLine("{0} words in text:", words.Length);

        foreach (string s in words)
        {
            System.Console.WriteLine(s);
        }


Moderiert von user profile iconUGrohne: Beitragsformatierung überarbeitet.
DareDevil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Mi 18.04.07 16:16 
soweit bin ich ja auch schon nur da wird dann halt nicht das ",.:" mit ausgegeben
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mi 18.04.07 18:40 
Kurz und knapp: Selbst schreiben ;) . Selbst wenn es solch eine Funktion irgendwo im Internet gäbe, wird die Suche wohl mindestens so viel Zeit wie das Neuschreiben in Anspruch nehmen.
korken
Hält's aus hier
Beiträge: 5



BeitragVerfasst: Mi 18.04.07 20:06 
Damit gehts:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
            char[] delimiterChars = { ' '',''.'':''\t' };
            string text = "one\ttwo three:four,five six seven";
            System.Console.WriteLine("Original text: '{0}'", text);

            ArrayList words = new ArrayList();
            int index = text.IndexOfAny(delimiterChars);
            while (index > -1)
            {
                words.Add(text.Substring(0, index+1));
                text = text.Substring(index + 1);
                index = text.IndexOfAny(delimiterChars);
            }
            words.Add(text);
            System.Console.WriteLine("{0} words in text:", words.Count);
            
            IEnumerator Enumerator = words.GetEnumerator();
     while (Enumerator.MoveNext())
     {
                System.Console.WriteLine(Enumerator.Current);
            }


Moderiert von user profile iconUGrohne: Code- durch C#-Tags ersetzt
DareDevil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Mi 18.04.07 21:27 
jo funktioniert thx
habe aber auch schon selber geschrieben
glaube meins ist eher über 3 ecken gemacht

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
if (input != "")
{
  bool breakSplit = false;
        for (int i = 0; i < input.Length; i++)
        {
          switch (input[i])
                {
                  case '+':
                          refStringList.Add(input.Substring(0, i));
        refStringList.Add("+");
        input = input.Remove(0, i + 1);
        breakSplit = true;
        break;
      case '-':
        refStringList.Add(input.Substring(0, i));
        refStringList.Add("-");
        input = input.Remove(0, i + 1);
        breakSplit = true;
        break;
      case '*':
        refStringList.Add(input.Substring(0, i));
        refStringList.Add("*");
        input = input.Remove(0, i + 1);
        breakSplit = true;
        break;
      case '/':
        refStringList.Add(input.Substring(0, i));
        refStringList.Add("/");
        input = input.Remove(0, i + 1);
        breakSplit = true;
        break;
      case '(':
        refStringList.Add(input.Substring(0, i));
        refStringList.Add("(");
        input = input.Remove(0, i + 1);
        breakSplit = true;
        break;
      case ')':
        refStringList.Add(input.Substring(0, i));
        refStringList.Add(")");
        input = input.Remove(0, i + 1);
        breakSplit = true;
        break;
    }
    if (breakSplit)
    {
      break;
    }
  }
  SplitString(input, ref refStringList);
}


unsere leherer will halt das wir es ohne schleife machen und soll halt für einen einfachen mathematischen parser sein
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Do 19.04.07 12:39 
user profile iconDareDevil hat folgendes geschrieben:
unsere leherer will halt das wir es ohne schleife machen und soll halt für einen einfachen mathematischen parser sein
Und deine Version ist ohne Schleife ;) ?
Da aller guten Vorschläge drei sind:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
    static IEnumerable<string> SplitEx(/*this*/ string input, params char[] seperators)
    {
      int position = 0;
      do {
        int find = input.IndexOfAny(seperators, position);
        if (find > -1) {
          yield return input.Substring(position, find - position);
          yield return input[find].ToString();
          position = find + 1;
        } else
          break;
      } while (true);
      if (position != input.Length)
        yield return input.Substring(position);
    }

@korken: Benutze bei Enumerators foreach, ist um Welten einfacher.
DareDevil Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 70

Windows7
C# (VS 2010)
BeitragVerfasst: Do 19.04.07 13:09 
ja so halb ich such halt die position vom operator

@Khabarakh

und wie muss ich jetzt deine funktion aufrufen

hat sich schon mit dem aufrufen erledigt

aber ich blicke bei dem quellecode trotzdem nicht durch
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Fr 20.04.07 16:04 
Wenn es um IEnumerable, yield und Konsorten geht, wirst du im SDK unter "Iterator" fündig werden (en: "Iterators (C# Programming Guide)"). Ziemlich geniale Spielzeuge, vor allem wirst du mit Enumerators unabhängig von der Speicherung der Rückgabedaten - der User kann entscheiden, ob er sie in eine Liste oder eine andere Collection speichern oder direkt per foreach ohne Zwischenspeicherung benutzen möchte. Ansonsten hier einmal meine Methode in deiner Vorgehensweise (geraten: dürfte etwas langsamer sein).
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
static IEnumerable<string> SplitEx(/*this*/ string input, params char[] seperators)
{
  int segmentStart = 0// Index des ersten Chars des nächsten Segments
  for (int i = 0; i < input.Length; i++)
    if (Array.IndexOf<char>(seperators, input[i])) {
      yield return input.Substring(segmentStart, i - segmentStart); // Segment bis zum Trenner ausgeben
      yield return input[i].ToString(); // Trenner ausgeben
      segmentStart = i + 1// nächstes Segment beginnt hinter Trenner
    }

  // falls noch ein nicht abgeschlossenes Segment übrig ist, auch dieses ausgeben
  if (segmentStart != input.Length)
    yield return input.Substring(segmentStart);
}
Die Kommentare lassen sich in etwa auch auf meine erste Version übertragen.