最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

if statement - javascript if condition check string with newline symbol i.e n - Stack Overflow

programmeradmin0浏览0评论

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');`

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');`
Share Improve this question edited Nov 1, 2013 at 6:17 aarav asked Oct 31, 2013 at 10:00 aaravaarav 2301 gold badge4 silver badges23 bronze badges 3
  • parsedarray[0] will be a string, not a character, so it can have 0, 1 or more characters. If there is a match on that condition, obviously the alert box will be blank, as the string has only one character, the newline. – Juan Sánchez Commented Oct 31, 2013 at 10:06
  • 1 parsedarray never contains a newline character , split gives the other parts of the string based on newline character – Bharath R Commented Oct 31, 2013 at 10:12
  • I want to remove the newline character and add add another string to that index,??? so that my array size should not grow. – aarav Commented Oct 31, 2013 at 10:15
Add a comment  | 

5 Answers 5

Reset to default 11

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")
}

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.

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

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>

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..

发布评论

评论列表(0)

  1. 暂无评论