Jump to content

How to deal with a multi lined string as a variable?


smus

Recommended Posts

My string variable is quite big and it contains line-breaks. I need to make it single lined to be detected by JS as usual string variable:

initial condition:

var str = "my

string";

the string without line breaks:

var str = "my string";

How can I detect those line breaks? By replacing '/n' symbols with spaces? Are there any other ways to deal with them?

Link to comment
Share on other sites

Hi, smus!

Is your goal to detect the line breaks in str, replace each with a space, and reassign the resulting string to str?

Also, are you certain about the nature of the linebreaks?  Do you know with certainty that they are created with /n?

Roddy

Edited by iwato
Link to comment
Share on other sites

4 minutes ago, iwato said:

Hi, smus!

Is your goal to detect the line breaks in str, replace each with a space, and reassign the resulting string to str?

Also, are you certain about the nature of the linebreaks?  Do you know with certainty that they are created with /n?

Roddy

Hello, iwato!

Well, basically, my goal is to make some manipulations with a string variable and it's ok with relatively small single line strings.

But when I copy and paste a text from a .txt file and assign it to str, of course, there is an error, because JS is a line-break sensitive.

So, I want to make multiline text become single line string programatically. For example, paste it to a textarea and then take it as a string from there.

This code doesn't change the string:

 var e = str.indexOf("/n");
     if(e != -1){    // if we have at least one line break in str, replace /n with space
        str = str.replace(/n/g," ");
     }

 

Link to comment
Share on other sites

If you want to detect that a string has a line break, you just search for "\n". In the following code, the variable "hasLineBreak" will be true when the string has a line break in it.

var hasLineBreak = str.indexOf("\n") >= 0;

You might want to split a string by its line breaks. The following code creates an array of strings, each string is one of the lines of the first string.

var lines = str.split("\n");

When printing a string to HTML, if you want the line breaks to still be visible, you have to replace them with <br> tags. Here's how you would do that:

var output = str.replace(/\n/g, "<br>");
element.innerHTML = output;

 

  • Like 1
Link to comment
Share on other sites

And then, if you want to replace each \n with a space, you simply write:

var output = str.replace(/\n/g, " ");
element.innerHTML = output;

Roddy

  • Like 1
Link to comment
Share on other sites

7 hours ago, justsomeguy said:

With most browsers that are not IE, you can also use a template literal:

 


var str = `text


more text

yet more text`;

Wow, it works! But what if I am receiving a text from a <textarea>? Is it possible to somehow insert those literals, so that JS 'thought' it is an integral string?

Link to comment
Share on other sites

If you're receiving the data from any source then you don't need to do anything special.  The only reason that there are special considerations for multiline strings is for when you need to define a variable with a multiline string.  If you're not writing Javascript code and defining these strings in variables then you don't need to do anything special with line breaks.  What's the purpose of this, are you programmatically writing a Javascript file or something?  I mean, you can get the value of any textarea and do whatever you want with that value, short of building a string of Javascript code using that value, without doing any special handling on the value.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...