javascript - Deleting last element from an array (deleting last row from string) - Stack Overflow

My textfield has this value:"1223445a678"When I split it by "n&qu

My textfield has this value:

"1  
2  2
3  
4  4
5  a
6  
7  
8
"

When I split it by "\n" I get:

["1", "2  2", "3", "4  4", "5  a", "6", "7", "8", ""]

I want to delete the last element. I'm doing this:

$('#textArrayId').val().replace($('#textArrayId').val().split("\n")[$('#textArrayId').val().split("\n").length - 1], "")

And I can't get it to delete. What to do?

I want to do this with string operations, not array operations.

My textfield has this value:

"1  
2  2
3  
4  4
5  a
6  
7  
8
"

When I split it by "\n" I get:

["1", "2  2", "3", "4  4", "5  a", "6", "7", "8", ""]

I want to delete the last element. I'm doing this:

$('#textArrayId').val().replace($('#textArrayId').val().split("\n")[$('#textArrayId').val().split("\n").length - 1], "")

And I can't get it to delete. What to do?

I want to do this with string operations, not array operations.

Share Improve this question edited Oct 12, 2012 at 15:37 petko_stankoski asked Oct 12, 2012 at 15:05 petko_stankoskipetko_stankoski 10.7k42 gold badges134 silver badges234 bronze badges 5
  • Why are you splitting by newline when that String uses spaces as a delimiter? – Jivings Commented Oct 12, 2012 at 15:08
  • @Jivings The strings are separated by newlines, but you need to put two spaces at the end of a line for the newline to show up in Markdown. – Niet the Dark Absol Commented Oct 12, 2012 at 15:10
  • Your latest edit is not in line with When I split by "\n" I get:. Please describe the exact result you want to get, given your input. – phant0m Commented Oct 12, 2012 at 15:16
  • @raina77ow I just want to delete the last element. The array to finish with "8" – petko_stankoski Commented Oct 12, 2012 at 15:21
  • Your question still isn't entirely clear. In the title it says: "delete last row of string", that would be the 8, but in your question says you want to remove the last element of the array, which is not the 8. – phant0m Commented Oct 12, 2012 at 15:33
Add a ment  | 

3 Answers 3

Reset to default 3

To remove the last line, use this

var lines = $('#textArrayId').val().split("\n");
var withoutLastLine = lines.slice(0, -1).join("\n");
$('#textArrayId').val(withoutLastLine);

Or if you want to remove the last line without whitespace:

var lines = $.trim($('#textArrayId').val()).split("\n");
var withoutLastLine = lines.slice(0, -1).join("\n");
$('#textArrayId').val(withoutLastLine);

To remove the last element of an array, use this expression to receive all elements but the last:

x.slice(0, -1)

It would be better to trim off the whitespace before splitting. That way you can handle input more robustly:

var lines = document.getElementById('textArrayId').value
               .replace(/^\s+|\s+$/g,'').split(/\s+/);

The regex used in the split will also allow for input like:

1    2
  3         4  5 6
 7    8

Also note: No jQuery used ^_^

you can use the bounded array like a[7] to store the value from 1 to 8 .

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745351191a4623842.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信