如何在EditText或String中的所有行中添加特殊字符


我希望你没事。我想用一种方法在每一行的开头(换行处(添加一个特殊字符,例如我有一行接一行的文本:
Paragraphs are the building blocks of papers. 
Many students define paragraphs in terms of length. 
a paragraph is a group of at least five sentences. 
a paragraph is half a page long, etc. In reality. 
though, the unity and coherence of ideas among. 
sentences is what constitutes a paragraph. 

我希望它是这样的(每行的开头都有"-"(:

-Paragraphs are the building blocks of papers. 
-Many students define paragraphs in terms of length. 
-a paragraph is a group of at least five sentences. 
-a paragraph is half a page long, etc. In reality. 
-though, the unity and coherence of ideas among. 
-sentences is what constitutes a paragraph. 

这是我的小代码:

String string = edittext1.getText().toString();
//here i want to add code for adding a "special character like -+*" in every new line
textview1.setText(string);

并提前感谢您。

以下是实现的简单方法

String string = edittext1.getText.toString; 
StringBuilder result = new StringBuilder(); 
String[] array = string.split("\n"); 
for(String line : array){ 
result.append("-").append(line).append("n"); 
}
textview1.setText(result);

老实说,你应该直接编辑字符串,但在这种情况下,可能会制作第三个字符串,并为该字符串将单独的文本行连接为字符串,字符串中包含&quot-"因此,现在您可以一次打印一行所需的字符串,\n以分隔行。

您可以根据需要将charSequence替换为新的charSequence来实现这一点。

Kotlin代码示例https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/replace.html

Java代码示例https://www.javatpoint.com/java-string-replace

最新更新