Reputation: 230
In my script I have a string with newline characters. Using newline (\n
)character I parsed it to array.
I struggling to compare in my if condition (parsedarray[0]=='newline character'
). Please help me to compare the newline character in my if condition. I also tried to alert the parsedarray[0]
. It alerts blank alert box.` I am unable to check the newline character in if statement. For instance, I have a single String with multiple newline , tabs consisting of 40 lines. In my script user enter a line number, string for that line number, after receiving both information, I want to replace the newline with entered string in the line number. Here line number is an index. So that again I will construct a single string by joining the parsed string. Also my array size should not grow. or Reduce.??? And importantly I want to validate the given String with the available string, if both matches (other than newline) need to put alert message.
var strarray=doc.getElementbyid('mytextarea').value;
var parsedarray=[];
parsedarray=strarray.split('\\n');`
Upvotes: 2
Views: 30552
Reputation: 230
I recreated the array like adding a delimiter var one=document.forms[0]["mytextarea"].value;
myarray=one.split('\|');
after each newline character. Then I split the string using the delimiter. Then onwards I can easily accessing the newline \n
character by each index. After replacing the string on specific index, again i construct a single string by joining them using same delimiter. var two=myarray.join('|');
Also my alertbox shows now \n
, when I tried to print the index. Thanks for all your contribution..
Upvotes: 0
Reputation: 1531
You can do something like this
<html>
<head>
<script>
function myFunction()
{
var strarray=document.getElementById("tre").value;
var eachLine = strarray.split('\\n');
alert('Lines found: ' + eachLine.length);
for(var i = 0, l = eachLine.length; i < l; i++) {
//do add another string to the index here
alert('Line ' + (i+1) + ': ' + eachLine[i]);
}
</script>
</head>
<body>
<textarea id="tre"></textarea>
<button onclick="myFunction()">Click me</button>
</body>
</html>
Upvotes: 0
Reputation: 6787
The split
method will split your string into an array of strings that were separated by the character you passed as parameter. For example:
var text = 'foo\nbar\nbaz',
splitted = text.split('\n');
console.log(splitted); // ["foo", "bar", "baz"]
If you want to replace your new line character with another string, you can use the replace
method.
var text = 'foo\nbar\nbaz',
replaced = text.replace(/\n/g, '-');
console.log(replaced); // foo-bar-baz
Upvotes: 0
Reputation: 5402
Instead of split
use indexOf
to find position of the first occurrence of a specified value in a string.
Eg:
var text="Hello world\n";
if(text.indexOf("\n")==-1){
alert("No newline characters")
}else{
alert("Contains newline characters")
}
Upvotes: 11
Reputation: 1716
String.split() will not include the delimiter in the resulting array. If you want to find the occurrence of a newline character, I would suggest using String.indexOf("\n")
instead.
Upvotes: 1