javascript javascript字符串变量的字符限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13697500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Character limit of a javascript string variable
提问by Rohith
Can JavaScript string store 100K characters? I've written a script where a string from PHP is passed to a variable in JavaScript. It works fine when it is cut short to almost ten thousand characters but breaks the script when attempting to pass the entire string whose length is a bit greater than 100K. No errors could be found though. Is there any solution for this as to any way of increasing character limit of JavaScript variable? I'm just a beginner. Would appreciate is some one could find a solution for this.
JavaScript 字符串可以存储 100K 个字符吗?我编写了一个脚本,其中将来自 PHP 的字符串传递给 JavaScript 中的变量。当它被缩短到几乎一万个字符时它工作正常,但在尝试传递长度略大于 100K 的整个字符串时会破坏脚本。虽然没有发现错误。对于增加 JavaScript 变量的字符限制的任何方式,是否有任何解决方案?我只是一个初学者。希望有人可以为此找到解决方案。
采纳答案by Aaron Lee
There is no theorical limit to JS or PHP on the size of their strings.
JS 或 PHP 的字符串大小没有理论上的限制。
I think there are a few possible situations.
我认为有几种可能的情况。
Firstly, check that you are not sending your string via HTTP GET. There is a maximum size for GET and i think its dependent on your web server.
首先,检查您是否没有通过 HTTP GET 发送您的字符串。GET 有一个最大大小,我认为它取决于您的 Web 服务器。
Secondly, if you do use POST, check in php.ini for post_max_size and see if it is smaller than the string size you are sending to it as well as your .htacccess file to see if php_value post_max_size is not too small.
其次,如果您确实使用 POST,请检查 php.ini 中的 post_max_size 并查看它是否小于您发送给它的字符串大小以及您的 .htacccess 文件,以查看 php_value post_max_size 是否不是太小。
Thirdly, check that in php.ini that your memory_limit does not restrict the size of memory that your script can use.
第三,在 php.ini 中检查您的 memory_limit 没有限制您的脚本可以使用的内存大小。
hope this helps.
希望这可以帮助。
回答by Charles
The ECMAScript Standard ECMA-262(6th Edition, June 2015) says
ECMAScript 标准ECMA-262(第 6 版,2015 年 6 月)说
6.1.4 The String Type
The String type is the set of all ordered sequences of zero or more 16-bit unsigned integer values ("elements") up to a maximum length of 253-1 elements.
6.1.4 字符串类型
String 类型是由零个或多个 16 位无符号整数值(“元素”)组成的所有有序序列的集合,最大长度为 2 53-1 个元素。
So don't plan on using more than 9,007,199,254,740,991 or about 9 quadrillion characters. Of course, you should be prepared for systems which cannot allocate 18 PB chunks of memory, as this is not required for conforming ECMAScript implementations.
所以不要计划使用超过 9,007,199,254,740,991 或大约 9 千万个字符。当然,您应该为无法分配 18 PB 内存块的系统做好准备,因为这不是符合 ECMAScript 实现的要求。