简体   繁体   中英

Removing character and adding new line

I want remove a character in a string and add a new line. How do I do that?

For example if there is a string like

"are you ready?~Here we go"

it should be written like "are you ready?*newline*Here we go"

I have clear logic of doing this by using charAt but I dont know how to remove the ~ character from the string.

Can anyone help?

If you want to replace the char ~ you just can use the java function replaceAll , or replaceFirst , if you want to that just for the first occurrence :

String my_new_str = my_str.replaceAll("~", "\n");

Hope it helped.

String#replace will do

String data="are you ready?~Here we go";    
data=data.replace("~", "\n");

You can use a Regular expression or replaceAll method if you want to replace all ~ characters with new line. For new line - use line.separator property.

System.getProperty("line.separator").

String newLine = System.getProperty("line.separator");
s.replaceAll("~", newLine);

Try this

    String s = "are you ready?~Here we go";
    s = s.replace("~", "\n");
    System.out.println(s);

isn't it simple and straight forward?

String new = "are you ready?~Here we go".replace("~", "\\n");

You can do:

String myStr = "are you ready?~Here we go";
myStr = myStr.replaceAll("~", "\n");

find the character(~) & replace your character(\\n)

your_string = your_string.replace('~','\n')

hope this may help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM