How to handle Multiple line strings in JavaScript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rogersands
    New Member
    • Jul 2015
    • 2

    How to handle Multiple line strings in JavaScript?

    Hi

    I have stored some vaue that I am getting from textarea to my mysql db like this

    "
    Some Data,Some More Data
    some Extra data
    some More Extra data
    "

    and I am getting that data in JSP page
    and I try to access that data in my JSP page using JavaScript

    like this
    var someValue=
    "
    "
    Some Data,Some More Data
    some Extra data
    some More Extra data
    "

    but there is multiple line split problem of string
    I try to replace by something like this
    someValue.repla ce("\n","");

    and that is not working

    any help on this topic
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    The might be just linefeeds ("\r") in the text.....

    Did you try this too:
    Code:
    .replace(/\n|\r/,"#")
    if this does not help do this to determince which characters are in the text:
    Code:
    v="Hallo\r\n"
    s=""
    for (i=0; i<v.length; i++) { s = s + v.charCodeAt(i); }
    The output of this example should be:
    "72971081081111 310"

    Comment

    • rogersands
      New Member
      • Jul 2015
      • 2

      #3
      @Luuk

      Thanks for replying

      Before I can Post first I would like to know If you have to store value of textarea fields in database and preserve all the line breaks that user has typed

      and display back that data back in JSP page with JavaScript what will be your approach for this?

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        "JSP page with JavaScript "

        JSP = JavaServer Pages

        - Java is not equal to JavaScript
        - I do not have (enough) knowledge about JSP
        - I would display it so the it will look the same ;)

        Comment

        • Sherin
          New Member
          • Jan 2020
          • 77

          #5
          Try This Code
          Code:
          <!doctype html>
          	<head>
          		<title>Multi-line JavaScript Strings</title>
          	</head>
          	<body>
          		<div id="var1"></div>
          		<div id="var2"></div>
          		<div id="var3"></div>
          
          		<script>
          			/* JS comes here */
                      var myStr1 = `This is my
                          multiline String
                          which is amazing`;
                      // adding string value to HTML tag 
                      document.getElementById('var1').innerHTML = myStr1;
                      
                      var myStr2 = "This is my                 multiline String                 using backslash to escape new line";
                      // adding string value to HTML tag 
                      document.getElementById('var2').innerHTML = myStr2;
                      
                      var myStr3 = "This is my" +
                          " multiline String " +
                          " using concatenation technique";
                      // adding string value to HTML tag 
                      document.getElementById('var3').innerHTML = myStr3;
                      
          		</script>
          	</body>
          </html>
          OUTPUT

          Code:
          This is my multiline String which is amazing
          This is my multiline String using backslash to escape new line
          This is my multiline String using concatenation technique

          Comment

          Working...