如何删除 TextView 的最后一行?
How to remove last line of TextView?
我正在尝试 remove\replace TextView
的最后一行,但是 我想要一种更快地完成此操作的方法。
例如我发现这个:
String temp = TextView.getText().toString();
temp.substring(0,temp.lastIndexOf("\n"));
但我想在不将数据从 Textview 复制到字符串值的情况下更快地完成它,并且不使用 Sting.lastIndexOf
(因为它在字符串中搜索)。
有人能帮帮我吗?
我的问题不是重复问题,因为我想要一种不使用字符串类型的方法!!!
使用 System.getProperty("line.seperator")
代替“\n”
public void removeLastLine(TextView textView) {
String temp = textView.getText().toString();
textView.setText(
temp.substring(0, temp.lastIndexOf(System.getProperty("line.seperator") )));
}
试试这个:
public String removeLastParagraph(String s) {
int index = s.lastIndexOf("\n");
if (index < 0) {
return s;
}
else {
return s.substring(0, index);
}
}
并像这样使用它:
tv.setText(removeLastParagraph(tv.getText().toString().trim());
我建议用您自己的实现覆盖 TextView
class:
public class CustomTextView extends TextView{
// Overwrite any mandatory constructors and methods and just call super
public void removeLastLine(){
if(getText() == null) return;
String currentValue = getText().toString();
String newValue = currentValue.substring(0, currentValue.lastIndexOf("\n"));
setText(newValue);
}
}
现在您可以使用以下内容:
CustomTextView textView = ...
textView.removeLastLine();
或者,由于某种原因您似乎正在寻找单线而不创建 String temp
,您可以这样做:
textView.setText(textView.getText().toString().replaceFirst("(.*)\n[^\n]+$", ""));
正则表达式解释:
(.*) # One or more character (as capture group 1)
\n # a new-line
[^\n] # followed by one or more non new-lines
$ # at the end of the String
# Replace it with the capture group 1 substring
# (so the last new-line, and everything after it are removed)
我正在尝试 remove\replace TextView
的最后一行,但是 我想要一种更快地完成此操作的方法。
例如我发现这个:
String temp = TextView.getText().toString();
temp.substring(0,temp.lastIndexOf("\n"));
但我想在不将数据从 Textview 复制到字符串值的情况下更快地完成它,并且不使用 Sting.lastIndexOf
(因为它在字符串中搜索)。
有人能帮帮我吗?
我的问题不是重复问题,因为我想要一种不使用字符串类型的方法!!!
使用 System.getProperty("line.seperator")
代替“\n”
public void removeLastLine(TextView textView) {
String temp = textView.getText().toString();
textView.setText(
temp.substring(0, temp.lastIndexOf(System.getProperty("line.seperator") )));
}
试试这个:
public String removeLastParagraph(String s) {
int index = s.lastIndexOf("\n");
if (index < 0) {
return s;
}
else {
return s.substring(0, index);
}
}
并像这样使用它:
tv.setText(removeLastParagraph(tv.getText().toString().trim());
我建议用您自己的实现覆盖 TextView
class:
public class CustomTextView extends TextView{
// Overwrite any mandatory constructors and methods and just call super
public void removeLastLine(){
if(getText() == null) return;
String currentValue = getText().toString();
String newValue = currentValue.substring(0, currentValue.lastIndexOf("\n"));
setText(newValue);
}
}
现在您可以使用以下内容:
CustomTextView textView = ...
textView.removeLastLine();
或者,由于某种原因您似乎正在寻找单线而不创建 String temp
,您可以这样做:
textView.setText(textView.getText().toString().replaceFirst("(.*)\n[^\n]+$", ""));
正则表达式解释:
(.*) # One or more character (as capture group 1)
\n # a new-line
[^\n] # followed by one or more non new-lines
$ # at the end of the String
# Replace it with the capture group 1 substring
# (so the last new-line, and everything after it are removed)