-2

I am trying to make a minecraft color code parser. I take the original text from a textbox and display the resulting text in the rich textbox. The original text would look something like this:

&4Red

Here, &4 means the text after that should be red color.

abc&4Red&fWhite

This text should look like abc in default color (black), "Red" in red, "White" in white.

So how can I parse the text to separate the text to "abc", "&4Red" and "&fWhite" ?

wingerse
  • 3,670
  • 1
  • 29
  • 61
  • What problems are you having? Have you tried anything at all yet? – Corey Ogburn Mar 24 '15 at 19:53
  • Of course, I tried but I have no idea how to separate pieces of text to like "abc","&4Red" and "&fWhite" in the above text. – wingerse Mar 24 '15 at 19:55
  • 1
    If you need help parsing strings, ask about parsing strings. Your question right now sounds as if you're asking us to make this for you. – Corey Ogburn Mar 24 '15 at 19:57
  • Ok now. I have thought a lot on ways to make it separated but got no idea. – wingerse Mar 24 '15 at 20:00
  • 1
    Well, delete this question, go try one of the ways you thought of, if it works great! If it did not then come back here and ask a new question showing what you tried and explain what went wrong. Did you even try googling "Split string C#" yet? – Scott Chamberlain Mar 24 '15 at 20:01
  • You could try [String.Split()](https://msdn.microsoft.com/en-us/library/system.string.split%28v=vs.110%29.aspx) or [String.IndexOf()](https://msdn.microsoft.com/en-us/library/k8b1470s(v=vs.110).aspx) or [Regular Expressions](https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx) – Corey Ogburn Mar 24 '15 at 20:03
  • 1
    I am going to give you a little general advice about programming, break your problem down in to its smallest parts and solve them individually. You have three problems 1)How do I split `abc&4Red&fWhite` in to `abc`, `&4Red`, and `&fWhite`. 2)How do I strip off `` off of a text then store the color the number represented with the text 3)How do I display colored text in a rich text box. **Focus on one problem at a time**, write up a simple program and just try to get the string splitting part, then try doing just the parsing part, then try doing just the display part. – Scott Chamberlain Mar 24 '15 at 20:06

1 Answers1

3

You will have to learn about

  1. basic C#
  2. Regex
  3. LINQ
  4. Anonymous types
  5. Delegates

but this does the trick :

        var txt = "abc&4Red&fWhite";

        // Add color code Black for first item if no color code is specified
        if (!txt.StartsWith("&"))
            txt = "&0" + txt;

        // Create a substrings list by splitting the text with a regex separator and
        // keep the separators in the list (e.g. ["&0", "abc", "&4", "Red", "&f", "White"])
        string pattern = "(&.)";
        var substrings = Regex.Split(txt, pattern).Where(i => !string.IsNullOrEmpty(i)).ToList();

        // Create 2 lists, one for texts and one for color codes
        var colorCodes = substrings.Where(i => i.StartsWith("&")).ToList();
        var textParts = substrings.Where(i => !i.StartsWith("&")).ToList();

        // Combine the 2 intermediary list into one final result
        var result = textParts.Select((item, index) => new { Text = item, ColorCode = colorCodes[index] }).ToList();
Bruno
  • 4,685
  • 7
  • 54
  • 105