how to split String based on \r\n [duplicate] - javascript

This question already has answers here:
How to split newline
(13 answers)
Closed 7 years ago.
We are Developing phonegap application.We get Data form CSV file. It's look like this
We need Data Split into two strings Like
String1
String2
We tried like this but We don't have luck so Please guide me
var split = string.split('\r\n');
Please help me

try this:
var split = string.split(/\n/);

Replace the new line characters with space and then split the string with space
string.replace( /\n/g, " " ).split(" ");
UPDATE:
var string1=string.substring(0,string.indexOf("TOTALAMOUNT"));
var string2=string.substring(string.indexOf("TOTALAMOUNT"),string.length);
Or if your string contains \n then:
var string1=string.substring(0,string.indexOf("\n"));
var string2=string.substring(string.indexOf("\n"),string.length);
alert(string1);
alert(string
Fiddle

Related

How can I replace or delete a character from a string in React JS like normal Javascript? [duplicate]

This question already has answers here:
Replace all occurances in javascript
(4 answers)
Closed 2 years ago.
I have a string that contains multiple '-'. I want to remove the all '-'. I tried the following way but it didn't work.
var str = "baa7f3b17ffc-4216-bfbc-8e9f70f26984"
var new_str = str.replace('-', '')
How can I remove or replace all the '-'? Is there any simple function for that?
Remove globally and remove the second parenthesis
var str = "baa7f3b17ffc-4216-bfbc-8e9f70f26984";
var new_str = str.replace(/-/g, '');
console.log(new_str);

find and replace '%20' with a space in a string javascript [duplicate]

This question already has answers here:
Javascript replace all "%20" with a space
(7 answers)
Closed 4 years ago.
I'm having some trouble trying to figure this out,
basically I have a url string like so this%20is%20a%20string now what I want to do is find and replace all instances of %20 and replace with a space so the string then becomes this is a string.
Now I've tried to do something like this..
if(string.includes('%20')) {
const arr = str.split('%20');
}
which splits the string into an array, but I'm not sure how I can then turn the array of seperate strings into a full string with spaces between each word.
Any help would be appreciated.
Using regex,
str.replace(/%20/g, ' ');
Just use join:
str.split('%20').join(" ")
let val = "this%20is%20a%20string".replace(/%20/g, ' ');
alert(val);
replace

How to replace the backward slash from string? [duplicate]

This question already has answers here:
JavaScript: A BackSlash as part of the string
(3 answers)
Closed 4 years ago.
What is wrong with the following code?
Expected output : substr1#substr2#substr3
var str = "substr1\substr2\substr3"
// it works if I use the double slash "\\" in thestring but not with single.
console.log(str.replace(/\\/g, "#"));
Your initial string itself do not have a backslash. To verify check the snippet below:
var str = "substr1\sustr2\substr3"
console.log(str);
The actual output you expect can be obtain by first escaping the backslash and then replacing it with #:
var str = "substr1\\sustr2\\substr3"
console.log(str.replace(/\\/g, "#"));

JavaScript how to strip/remove a char from string [duplicate]

This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I'm looking on how to remove a char from a string for example let's say i have "#22UP0G0YU" i want it to remove the # from it how would i do? I also have a small little other question too about how to make string upper case as well thanks in advance.
To remove a specific char I normally use replace, also good for a set of chars:
var str = '#22UP0G0YU';
var newString = str.replace('#', ''); // result: '22UP0G0YU'
To Uppercase, just use .toUpperCase();
var str = '#22UP0G0yu';
var newString = str.replace('#', '').toUpperCase(); // result: '22UP0G0YU'

To add a new line into the JavaScript string [duplicate]

This question already has answers here:
How do I create a new line in Javascript?
(18 answers)
Closed 5 years ago.
To add a new line into a JavaScript string can I use the following code?
Is it according to programming standards? Is there another way to do this?
For example,
var string = "check";
alert(string);
var newstring = string + "\n\n";
alert(newstring);
I think it is fine. If you are using alert for the debugging purposes, Please use
console.log("string + "\n"); instead.
\n
is the right way to handle new lines in alert boxes.
Simply add a \n everytime you want a new line in your string :
var str = 'Test\nTest2\nTest3';
alert(str);

Categories

Resources