我坚持从字符串数组 在这里得到每个字是部分代码:获取每个字
String wordS[] = mystring.split(" ");
那么我想获得在放养每个字字[],并经过转换在字放养每个字[]转换成String
例如:
mystring = " hi everyone how are you "
String[] wordS = mystring.split(" ");
String word1 = word1 from wordS[](Hi)
String word2 = word2 from wordS[](eeryone)
String word3 = word3 from ... for max 20 strings wich contain 1 word
编辑:
这里是我的代码:
String txt = tv.getText().toString();
Pattern mat = Pattern.compile("(?<=\\^)([^0-9]+[0-9]+)|([^^0-9]+$)");
Matcher m = mat.matcher(txt);
while (m.find())
{
String match = m.group();
String n= match.replaceFirst("^[^0-9]+", "");
if (n.length() >= 1)
{
int i = Integer.parseInt(n);
switch (i)
{
case 0:
s = "<font color ='black'>";
break;
case 1:
s = "<font color ='red'>";
break;
case 2:
String green = "#7CFC00";
s = "<font color ='" + green + "'>";
break;
case 3:
String gold ="#FFD700";
s = "<font color ='" + gold + "' >";
break;
case 4:
s = "<font color ='blue' >";
break;
case 5:
String cyan ="#00FFFF";
s = "<font color ='" + cyan + "' >";
break;
case 6:
String magenta ="#FF00FF";
s = "<font color ='" + magenta + "' >";
break;
case 7:
String peru ="#FFC0CB";
s = "<font color ='" + peru + "' >";
break;
case 8:
String gray ="#808080";
s = "<font color ='" + gray + "' >";
break;
case 9:
String orange ="#FFA500";
s = "<font color ='" + orange + "' >";
break;
default:
s = "<font color ='white'>";
}
String replace = match.replaceFirst("[0-9]+$", "");
String[] wordS = replace.split(" ");
showf.setText(Html.fromHtml(s + "" + replace + "</font>"));
}
else
{
showf.setText(match.replaceFirst("[0-9]+$", ""));
}
}
此功能上色字符串的一部分,如果用户键入的它将检测^ TEST2 < - “2” = INT其分配颜色。如果Y型:^ blablabla5它将返回青色文字颜色。工作正常,但现在我想更新我的功能的例子,如果我输入:^ word4^secondword6 我的功能将只返回最后一个单词,但我希望由用户输入的所有单词 有你想法吗?
与数组中的元素一样创建尽可能多的变量有什么意义?事实上,你不能那样做。您无法在运行时创建变量。 –