gpt4 book ai didi

java - 从字符串数组+颜色词中获取每个单词

转载 作者:行者123 更新时间:2023-12-01 14:10:54 25 4
gpt4 key购买 nike

我坚持从字符串数组中获取每个单词这是部分代码:

String wordS[] = mystring.split(" ");

然后我想获取 wordS[] 中存储的每个单词,然后将 wordS[] 中存储的每个单词转换为 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我的函数只会返回最后一个单词,但我想要用户输入的所有单词请问你有什么想法吗?

最佳答案

纠正字符串数组的使用

String [] words = mystring.split(" ");

然后使用索引来获取每个字符串。例如:words[i],其中 i 是索引。因此,您可以只使用 words[0]words[1] 等而不声明添加,而不是为每个声明单独的 String变量。与执行以下操作相同:

String s1 = words[0];
String s2 = words[1];
...

关于在TextView中显示,不清楚您想要在TextView中显示什么。

TextView text = (TextView) findViewById(Your_Textviw_ID); // you  might have declared it in an XML somewhere. 
text.setText(the_sring_you_want_to_set_it_to);

关于java - 从字符串数组+颜色词中获取每个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18539900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com