0

In my Text Rendering system for my game, I've implemented support for coloured words. So, if I want to render a sentence with the first word in red, and the other words in white, I can do something like this:

"|3Hello |1there Steven!"

When the string is rendered, it searches for a | character, and then looks at the number afterwards, which corresponds to a certain colour, (3 = red, 1 = white) and then it colours the rest of the string in that colour.

I want to be able to remove the | character as well as the number after it.

When it looks at the message and sees the |3, I want it to set the colour to red, and then remove the |3. If I use a method that replaces all the | followed by a number after it, when it comes to the part with |1, it won't set the colour to white because the |1 isn't in the message anymore.

How can I do this?

3 Answers 3

2

| is a special character so you must escape it

How about this? (assuming there is only one number after the '|')

str=str.substring(0,str.indexOf("|")+2).replaceAll("[|]\\d+", "")+str.substring(str.indexOf("|")+2);
Sign up to request clarification or add additional context in comments.

7 Comments

Won't this replace all of them? As stated in the question, I can't do that.
It only replace | that preceeds by number(s). If you have |3Hello |1ther|e Steven!, then str=str.replaceAll("[\\|]+[0-9]", ""); gives you Hello ther|e Steven!
I understand that, but, what I mean is that I want it to see the |3, process that, then ONLY replace the |3, because if it replaces the |1 as well, then it can't process that when it gets to it.
ok, now I don't get it. you only want to replace the first occurrence of [\\|]+[0-9]?
I know, it's hard to understand. I'll edit my question to explain it.
|
2

You could use the following regex to match an | followed by one or more digits, then replace with empty string:

[|]\d+

Here is a sample. Here is some test code:

String test = "|3Hello |1there Steven!";
String replaced = test.replaceAll("[|]\\d+", "");
System.out.println(replaced);

2 Comments

And here's a link on how to use regular expressions (regexes) in Java: docs.oracle.com/javase/tutorial/essential/regex
Thanks for helping, although this wasn't exactly what I was looking for, I appreciate the help. :) +1
0

You can split the input text by "|" and check the 1st character, assuming you not expecting to use any other | in your text. Not sure how you want to display the colors, but here's an example using html:

public static void main(String[] args) {

    String[] TEXT_COLORS = { 
            "FF0000", // Red
            "FF7F00", // Orange
            "FFFF00", // Yellow
            "00FF00" // Green
            };

    String input = "|3Hello |1there Steven!";
    String[] coloredPhrases = StringUtils.split(input, "|");

    StringBuilder output = new StringBuilder();
    for (int i=0; i < coloredPhrases.length; i++) {
        String phrase = coloredPhrases[i];
        if (phrase.substring(0,1).matches("\\d")) {
            int colorIndex = Integer.parseInt(phrase.substring(0,1));
            output.append(String.format("<font color=#%s>%s</font>", TEXT_COLORS[colorIndex],phrase.substring(1)));
        } else {
            output.append(phrase);          
        }
    }

    System.out.print(output);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.