TextView在pargephraph中隐藏单词?

时间:2016-02-20 15:39:47

标签: android

我试图在段落中隐藏以'#'开头的单词。例如:

String first = "This word is #12 and #word ";

TextView t = (TextView) findViewById(R.id.textbox);
t.setText(first);

我想隐藏单词“12”和“单词”..

最欣赏

2 个答案:

答案 0 :(得分:1)

FlyingPumba的回答中,如果您使用拆分("")而不是拆分('')将正常工作。还有另一种方法可以实现它。

String first = "This word is #12 and #word ";

StringTokenizer st = new StringTokenizer(first, " ");

StringBuilder sb = new StringBuilder();
while (st.hasMoreTokens()) {
    String s = st.nextToken();
    if (!s.startsWith("#")) {
        sb.append(s);
        sb.append(" ");
    }
}

TextView t = (TextView) findViewById(R.id.textbox);
t.setText(sb.toString());

答案 1 :(得分:0)

您可以使用以下内容:

String first = "This word is #12 and #word ";
String[] words = first.split(" ");
String result = "";
for (String word : words) {
    if (!word.startsWith("#")) {
        result += word;
        result += " ";
    }
}

TextView t = (TextView) findViewById(R.id.textbox);
t.setText(result);