遍历一个字符串并为每个字符添加不同的值作为前缀
Loop through a string and prefix each character with a different value
我正在使用 Bungeecord 为我的 Minecraft(我的世界)服务器开发一个命令,并且需要一种方法来将不同的颜色代码添加到字符串中每个字符的开头。希望我可以将他们输入的任何字符串变成彩虹色。我被要求使用 8 种不同的颜色代码,并为每 8 个通过的字符重复该序列。
我已经注册了命令和类似的一切并且它有效,这是我遇到问题的字符串操作。我知道要遍历一个字符串我可以使用
String message;
for (int i = 0; i < message.toCharArray().size(); i++)
{
char c = message.charAt(i);
//Prefix code here
}
我也定义了我要使用的8种颜色
final String one = "4";
final String two = "6";
final String three = "e";
final String four = "a";
final String five = "b";
final String six = "9";
final String seven = "5";
final String eight = "d";
我需要的是可以接受字符串的东西,例如
这段文字现在是彩虹!
并将遍历每个字符并将“§one” - “§eight”放在前面为每个字母着色。
我试过使用 String#replace(string, string) 但是它不接受字符。我也尝试使用
String.replace("" + c, "§" + one + c);
然而这也不起作用。如果您能提供任何帮助 and/or 指点,我们将不胜感激 :) 谢谢
编辑:所需输出的样本行
用户做了 /rainbow 这个文字是彩虹!
输出:§4T§6h§ei§as §bt§9e§5x§dt ...等,但从§4
重新开始
处理这个问题的最简单方法是遍历每个字符,然后将其添加到 StringBuilder
以及前缀:
StringBuilder builder = new StringBuilder();
for(int i = 0; i < message.length(); i++) {
char currentLetter = message.charAt(i);
if(currentLetter == ' ') //skip spaces; optional
continue;
String prefix = ...; //grab a prefix
builder.append(prefix + currentLetter);
}
String coloredMessage = builder.toString();
如果将颜色存储在数组中会更容易。将颜色的数量存储在变量中,这样您就不会在循环中继续执行 colors.length
。
String [] colors = {"4", "6", "e", "a", "b", "9", "5", "d"};
int numColors = colors.length;
创建一个 StringBuilder:
StringBuilder builder = new StringBuilder();
遍历字符数组。为字符添加正确的颜色。如果您的消息字符串大于 8 个字符,i%numColors
将允许颜色数组返回到开头,因为您的数组当前有 8 个选项。
for(int i = 0; i < message.length(); i++){
builder.append("§");
builder.append(colors[i%numColors]);
builder.append(message.charAt(i));
}
然后在你的方法结束时,只是 return 整个构建器字符串。
return builder.toString();
如果你想要 space 或在每个字符之间换行,只需执行 builder.append ""
或 /n
.
我正在使用 Bungeecord 为我的 Minecraft(我的世界)服务器开发一个命令,并且需要一种方法来将不同的颜色代码添加到字符串中每个字符的开头。希望我可以将他们输入的任何字符串变成彩虹色。我被要求使用 8 种不同的颜色代码,并为每 8 个通过的字符重复该序列。
我已经注册了命令和类似的一切并且它有效,这是我遇到问题的字符串操作。我知道要遍历一个字符串我可以使用
String message;
for (int i = 0; i < message.toCharArray().size(); i++)
{
char c = message.charAt(i);
//Prefix code here
}
我也定义了我要使用的8种颜色
final String one = "4";
final String two = "6";
final String three = "e";
final String four = "a";
final String five = "b";
final String six = "9";
final String seven = "5";
final String eight = "d";
我需要的是可以接受字符串的东西,例如
这段文字现在是彩虹!
并将遍历每个字符并将“§one” - “§eight”放在前面为每个字母着色。
我试过使用 String#replace(string, string) 但是它不接受字符。我也尝试使用
String.replace("" + c, "§" + one + c);
然而这也不起作用。如果您能提供任何帮助 and/or 指点,我们将不胜感激 :) 谢谢
编辑:所需输出的样本行
用户做了 /rainbow 这个文字是彩虹!
输出:§4T§6h§ei§as §bt§9e§5x§dt ...等,但从§4
重新开始处理这个问题的最简单方法是遍历每个字符,然后将其添加到 StringBuilder
以及前缀:
StringBuilder builder = new StringBuilder();
for(int i = 0; i < message.length(); i++) {
char currentLetter = message.charAt(i);
if(currentLetter == ' ') //skip spaces; optional
continue;
String prefix = ...; //grab a prefix
builder.append(prefix + currentLetter);
}
String coloredMessage = builder.toString();
如果将颜色存储在数组中会更容易。将颜色的数量存储在变量中,这样您就不会在循环中继续执行 colors.length
。
String [] colors = {"4", "6", "e", "a", "b", "9", "5", "d"};
int numColors = colors.length;
创建一个 StringBuilder:
StringBuilder builder = new StringBuilder();
遍历字符数组。为字符添加正确的颜色。如果您的消息字符串大于 8 个字符,i%numColors
将允许颜色数组返回到开头,因为您的数组当前有 8 个选项。
for(int i = 0; i < message.length(); i++){
builder.append("§");
builder.append(colors[i%numColors]);
builder.append(message.charAt(i));
}
然后在你的方法结束时,只是 return 整个构建器字符串。
return builder.toString();
如果你想要 space 或在每个字符之间换行,只需执行 builder.append ""
或 /n
.