Baustein
Baustein

Reputation: 49

Java Placeholder in already given String

So for my current Bukkit (Minecraft) plugin, I have to work with ItemStacks' names. I get these names by doing

item.getItemMeta().getDisplayName();.

Since the Items represent players, they have the players' names. But unfortunately, there are also color codes. The result I get when receiving the name then is something like "§7Baustein", where "§7" is the color code and "Baustein" is the player's name.

Now the color code can also be "§a" or "§3" or anything like that. To get only the player's name, I have to remove that color code out of the string I get.

I know how to remove the "§" since it's always the same: string.replaceAll("§", "");. But then there's as well this hexadecimal digit behind it, and I don't know what specific digit it is.

Rather than checking all possibilities, is there a way to kind of replaceAll("§" + the digit behind, "");?

I've checked out questions like this one, but either I did not really get it, or it's just not including my answer.

Maybe someone can help me?

Upvotes: 0

Views: 181

Answers (2)

Gondy
Gondy

Reputation: 5305

Yes. Check out Regular Expressions.

This

string.replaceAll("§[0-9a-fA-F]", "")

will remove § and one hexadecimal digit from your string.

Upvotes: 1

dehlen
dehlen

Reputation: 7391

If it is always exactly the first two characters you want to remove you could use:

public String removeColorCode(String s){
return s.substring(2);
}

Upvotes: 1

Related Questions