字符串长度和换行符 [英] string length and newlines

查看:405
本文介绍了字符串长度和换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为textarea执行客户端输入验证

确定字符数不超过某个

长度。目前,我只使用str.length,但如果textarea

包含换行符,则str.length是不准确的。如果我查看字符串,我会看到类似''123'\\ n '456'的内容,但是当它被传递回

服务器时,''\ n''将被更改为''\\\\ n'',而长度为7

的字符现在的长度为8.是解决此问题的最佳方法。 />
在字符串中搜索换行符并在长度计数上加1或者

有更简单的方法吗?


感谢您的帮助,

Rob

I am trying to perform client-side input validation for a textarea to
determine that the number of characters doesn''t exceed a certain
length. Currently, I am just using str.length, but if the textarea
contains newlines, str.length is inaccurate. If I view the string, I
will see something like ''123\n456'', but when this gets passed back to
the server, ''\n'' will be changed to ''\r\n'' and what had a length of 7
characters now has a length of 8. Is the best way to approach this to
search the string for newlines and add 1 to the length count or is
there a simpler way?

Thanks for your help,
Rob

推荐答案

1月10日,8:38 * am,Rob< rdenn ... @ triactive。 comwrote:
On Jan 10, 8:38*am, Rob <rdenn...@triactive.comwrote:

我正在尝试为textarea执行客户端输入验证

确定字符数不对超过一定长度的b $ b。 *目前,我只使用str.length,但如果textarea

包含换行符,则str.length是不准确的。 *如果我查看字符串,我会看到类似''123'\\ n '456'的内容,但是当它被传回

服务器时,''\\ \\ n''将更改为''\\\\ n'',而长度为7

的字符现在的长度为8. *这是解决此问题的最佳方法

在字符串中搜索换行符并在长度计数上加1或者

有更简单的方法吗?


感谢您的支持帮助,

Rob
I am trying to perform client-side input validation for a textarea to
determine that the number of characters doesn''t exceed a certain
length. *Currently, I am just using str.length, but if the textarea
contains newlines, str.length is inaccurate. *If I view the string, I
will see something like ''123\n456'', but when this gets passed back to
the server, ''\n'' will be changed to ''\r\n'' and what had a length of 7
characters now has a length of 8. *Is the best way to approach this to
search the string for newlines and add 1 to the length count or is
there a simpler way?

Thanks for your help,
Rob



我能想到的最紧凑的代码是:


var num_newlines = some_string.split(" \ n")。length - 1;

var augmented_length = some_string.length + num_newlines;


-Joey

The most compact code I can think of would be:

var num_newlines = some_string.split("\n").length - 1;
var augmented_length = some_string.length + num_newlines;

-Joey


2008年1月10日上午11:47, jh *** ***@gmail.com 写道:
On 1/10/2008 11:47 AM, jh******@gmail.com wrote:

1月10日上午8:38,Rob< rdenn ... @ triactive.comwrote:
On Jan 10, 8:38 am, Rob <rdenn...@triactive.comwrote:

>我正在尝试对textarea执行客户端输入验证,以确定字符数不超过某个
长度。目前,我只使用str.length,但如果textarea
包含换行符,则str.length是不准确的。如果我查看字符串,我会看到类似''123'\\ n '456'的内容,但是当它被传回服务器时,''\ n''将被更改为' '\\ n'和7长度的字符现在长度为8.是解决此问题的最佳方法
在字符串中搜索换行符并将1添加到长度计算还是
有一个更简单的方法吗?

感谢您的帮助,
Rob
>I am trying to perform client-side input validation for a textarea to
determine that the number of characters doesn''t exceed a certain
length. Currently, I am just using str.length, but if the textarea
contains newlines, str.length is inaccurate. If I view the string, I
will see something like ''123\n456'', but when this gets passed back to
the server, ''\n'' will be changed to ''\r\n'' and what had a length of 7
characters now has a length of 8. Is the best way to approach this to
search the string for newlines and add 1 to the length count or is
there a simpler way?

Thanks for your help,
Rob



最紧凑我能想到的代码是:


var num_newlines = some_string.split(" \ n")。length - 1;

var augmented_length = some_string.length + num_newlines;


-Joey


The most compact code I can think of would be:

var num_newlines = some_string.split("\n").length - 1;
var augmented_length = some_string.length + num_newlines;

-Joey



some_string.replace(/ \ n / g /,'' '');


将用一个空格替换字符串中的换行符


-

anthony在我的宠物程序员点com

some_string.replace(/\n/g/, '' '');

will replace the newlines in the string with a single space

--
anthony at my pet programmer dot com


在comp.lang.javascript消息< fm **** ******@registered.motzarella.org>

,星期四,2008年1月10日12:26:44,Anthony Levensalor

< ki **** **@mypetprogrammer.composted:
In comp.lang.javascript message <fm**********@registered.motzarella.org>
, Thu, 10 Jan 2008 12:26:44, Anthony Levensalor
<ki******@mypetprogrammer.composted:

> On 1/10/2008 11:47 AM, jh ****** @ gmail.com 写道:

> 1月10日上午8:38, Rob< rdenn ... @ triactive.comwrote:
>On Jan 10, 8:38 am, Rob <rdenn...@triactive.comwrote:

>>我正在尝试为textarea执行客户端输入验证
确定字符数不超过某个长度。目前,我只使用str.length,但如果textarea
包含换行符,则str.length是不准确的。如果我查看字符串,我会看到类似''123'\\ n '456'的内容,但是当它被传回服务器时,''\ n''将被更改为' '\\ n'和7长度的字符现在长度为8.是解决此问题的最佳方法
在字符串中搜索换行符并将1添加到长度计数还是
有一个更简单的方法?

感谢您的帮助,
Rob
>>I am trying to perform client-side input validation for a textarea to
determine that the number of characters doesn''t exceed a certain
length. Currently, I am just using str.length, but if the textarea
contains newlines, str.length is inaccurate. If I view the string, I
will see something like ''123\n456'', but when this gets passed back to
the server, ''\n'' will be changed to ''\r\n'' and what had a length of 7
characters now has a length of 8. Is the best way to approach this to
search the string for newlines and add 1 to the length count or is
there a simpler way?

Thanks for your help,
Rob


我能想到的最紧凑的代码将是:
var num_newlines = some_string.split(" \ n")。length - 1;
var augmented_length = some_string.length + num_newlines;

The most compact code I can think of would be:
var num_newlines = some_string.split("\n").length - 1;
var augmented_length = some_string.length + num_newlines;



这需要为每一行创建一个Object,这可能是一点点

慢。

That requires creating an Object for each line, which could be a little
slow.


> some_string.replace(/ \ n / g /,'''');

将用单个空格$替换字符串中的换行符b $ b
>some_string.replace(/\n/g/, '' '');

will replace the newlines in the string with a single space



如果第一次删除第一个斜杠。


如果行分隔总是包含与\ n的匹配,那么


X = some_string.replace(/ [^ \ n] / g,"")


应该给出新的数量线条,*可能*更快。未经测试。


-

(c)John Stockton,英国萨里。 ?@merlyn.demon.co.uk Turnpike v6.05 MIME。

Web< URL:http://www.merlyn.demon.co.uk/- FAQish主题,首字母缩略词,&链接。

正确< = 4行签名。如上所述的分隔符,一行恰好是 - 。 (SonOfRFC1036)

不要给我发邮件。在回复之前,请引用>或或 (SonOfRFC1036)

If the third slash is first removed.

If the line separation always contains a match to \n, then

X = some_string.replace(/[^\n]/g, "")

should give a count of new lines, *possibly* quicker. Untested.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "" (SonOfRFC1036)


这篇关于字符串长度和换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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