如何在android中拆分字符串



我正在从Online检索一个具有以下结构的Textview字符串。

这一行在特殊字符之前|这一行位于特殊字符之后

我希望在Mainactivity中添加Two Textview。将显示第一个文本视图"|"之前的第一行和"第二文本"视图将显示"|"之后的文本。

我该怎么做?

使用split()方法将行拆分为2,使用特殊字符|作为分隔符/delimeter,这将返回2个字符串的数组:

String[] splitted = line.split("\|");
textView1.setText(splitted[0]);
textView1.setText(splitted[1]);

line是原始字符串
或使用substring():

textView1.setText(line.substring(0, line.indexOf("|")));
textView1.setText(line.substring(line.indexOf("|") + 1));

这应该可以解决问题

yourTextView.setText("| " + textYouWantToAdd)

yourTextView.setText(textYouWantToAdd " |" +)

最新更新