Home > Back-end >  How to add paragraph in rest api including whitespaces?
How to add paragraph in rest api including whitespaces?

Time:05-25

I am fetching string from api and it looks like thisstring original format in console.log() but, when I use p tag to print it on website it become like this this is how it print on website

how do I maintain the original format of string and print on website

CodePudding user response:

You can modify your string to use HTML tags. For instance you use \  for white space. However if you want to convert your regular String to preserve its formatting in HTML, I wrote a method public static java.lang.String formatStringToPreserveIndentationForHtml(java.lang.String rawText) that converts your regular String into HTML formatted string that preserve the original format. Here is a javadoc for that method. The class TextUtils that has this method is part of Open source MgntUtils java library written and maintained by me. You can get it as maven artifact and on Github (including javadoc and source code)

CodePudding user response:

You can wrap a prefix and a postfix before and after your content before it is being posted to the server, so a string like

"   sdfjsgfhks    sdgsdjfsj    
    sdjksjh     sdkhsfs      "

would be converted to something like

"!^!   sdfjsgfhks    sdgsdjfsj    
    sdjksjh     sdkhsfs      !^!"

before it is posted. Then, the API side, fully aware of the wrapping would trim the prefix and postfix from the string it received.

  • Related