英文:
how to add special character to All Lines in EditText or in String?
问题
i hope you are fine.
i want a way to add a special character to begenning of every new Line (to the line break), for example i have this text line by line :
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.
and i want it to be like this (with "-" in every beginning of line) :
-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.
and this is my small code :
String string = edittext1.getText().toString();
//here i want to add code for adding a "special character like -+*" in every new line
textview1.setText(string);
and thank you in advance.
英文:
i hope you are fine.
i want a way to add a special character to begenning of every new Line (to the line break), for example i have this text line by line :
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.
and i want it to be like this (with "-" in every beginning of line) :
-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.
and this is my small code :
String string = edittext1.getText().toString();
//here i want to add code for adding a "special character like -+*" in every new line
textview1.setText(string);
and thank you in advance.
答案1
得分: 0
老实说,你应该直接编辑这些字符串,但在这种情况下,也许可以创建一个第三个字符串,将文本的各行作为字符串连接起来,其中使用一个包含“-”的字符串,这样你就可以逐行打印所需的字符串,每行用\n分隔。
英文:
Honestly, you should just be editing the strings directly, but in this case, maybe make a third-string and for that string concatenate the individual lines of text as strings with a string that contains "-" so now you can print your desired strings one line at a time with \n to separate the lines.
答案2
得分: 0
这是一个简单的方法来实现这个功能:
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);
英文:
Here is a simple way to do that
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);
答案3
得分: 0
你可以通过用你想要的新字符序列替换charSequence来实现此目标。
Kotlin代码示例
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/replace.html
Java代码示例
https://www.javatpoint.com/java-string-replace
英文:
You can achieve this by replacing the charSequence with new one as you want.
Kotlin Code Example
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/replace.html
Java Code Example
https://www.javatpoint.com/java-string-replace
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论