在javascript中删除一行文本 [英] Delete a line of text in javascript

查看:113
本文介绍了在javascript中删除一行文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中,如果我有这样的文本块

In javascript, If i have a text block like so

Line 1
Line 2
Line 3

我需要做些什么来删除第一行并将其转换为:

What would i need to do to lets say delete the first line and turn it into:

Line 2
Line 3


推荐答案

最简单的方法是使用拆分和连接函数,这样可以将文本块作为一个行数组来操作,比如所以:

The cleanest way of doing this is to use the split and join functions, which will let you manipulate the text block as an array of lines, like so:

// break the textblock into an array of lines
var lines = textblock.split('\n');
// remove one line, starting at the first position
lines.splice(0,1);
// join the array back into a single string
var newtext = lines.join('\n');

这篇关于在javascript中删除一行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆