- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想出了以下几点:
public static void cutString(String s) {
List<String> strings = new ArrayList<>();
int index = 0;
while (index < s.length()) {
strings.add(s.substring(index, Math.min(index + 1048576, s.length())));
index += 1048576;
}
}
但我的问题是,使用 UTF-8 某些字符并不完全占用 1 个字节,因此使用 1048576 告诉在哪里剪切字符串不起作用。我正在考虑使用 Iterator,但这似乎效率不高。对此最有效的解决方案是什么?字符串可以小于 1 Mb 以避免字符切片,但不能大于 1 Mb!
最佳答案
快速、不安全的破解
您可以使用 s.getBytes("UTF-8")
获取一个数组,其中包含每个 UTF-8 字符使用的实际字节数。像这样:
System.out.println("¡Adiós!".getBytes("UTF-8").length);
// Prints: 9
一旦有了它,只需将字节数组拆分为长度为 1048576 的 block ,然后使用 new String(chunk, "UTF-8")
.
但是,通过这样做,您可以在 block 的开头或结尾断开多字节字符。假设第 1048576 个字符是一个 3 字节的 Unicode 字符:第一个字节将进入第一个 block ,另外两个字节将被放入第二个 block ,从而破坏编码。
如果可以放宽“1 MB”的要求,可以采取更安全的方法:将字符串拆分为 1048576 个字符(不是字节)的 block ,然后使用 getBytes
测试每个 block 的实际长度, 根据需要从末尾删除字符,直到实际大小等于或小于 1 MB。
这是一个不会打断字符的实现,代价是让一些行小于给定的大小:
public static List<String> cutString(String original, int chunkSize, String encoding) throws UnsupportedEncodingException {
List<String> strings = new ArrayList<>();
final int end = original.length();
int from = 0, to = 0;
do {
to = (to + chunkSize > end) ? end : to + chunkSize; // next chunk, watch out for small strings
String chunk = original.substring(from, to); // get chunk
while (chunk.getBytes(encoding).length > chunkSize) { // adjust chunk to proper byte size if necessary
chunk = original.substring(from, --to);
}
strings.add(chunk); // add chunk to collection
from = to; // next chunk
} while (to < end);
return strings;
}
我用 chunkSize = 24
测试了它所以你可以看到效果。它应该适用于任何其他尺寸:
String test = "En la fase de maquetación de un documento o una página web o para probar un tipo de letra es necesario visualizar el aspecto del diseño. ٩(-̮̮̃-̃)۶ ٩(●̮̮̃•̃)۶ ٩(͡๏̯͡๏)۶ ٩(-̮̮̃•̃).";
for (String chunk : cutString(test, 24, "UTF-8")) {
System.out.println(String.format(
"Chunk [%s] - Chars: %d - Bytes: %d",
chunk, chunk.length(), chunk.getBytes("UTF-8").length));
}
/*
Prints:
Chunk [En la fase de maquetaci] - Chars: 23 - Bytes: 23
Chunk [ón de un documento o un] - Chars: 23 - Bytes: 24
Chunk [a página web o para pro] - Chars: 23 - Bytes: 24
Chunk [bar un tipo de letra es ] - Chars: 24 - Bytes: 24
Chunk [necesario visualizar el ] - Chars: 24 - Bytes: 24
Chunk [aspecto del diseño. ٩(] - Chars: 22 - Bytes: 24
Chunk [-̮̮̃-̃)۶ ٩(●̮̮] - Chars: 14 - Bytes: 24
Chunk [̃•̃)۶ ٩(͡๏̯͡] - Chars: 12 - Bytes: 23
Chunk [๏)۶ ٩(-̮̮̃•̃).] - Chars: 14 - Bytes: 24
*/
另一项使用 3 MB 字符串的测试,就像您在评论中提到的那样:
String string = "0123456789ABCDEF";
StringBuilder bigAssString = new StringBuilder(1024*1024*3);
for (int i = 0; i < ((1024*1024*3)/16); i++) {
bigAssString.append(string);
}
System.out.println("bigAssString.length = " + bigAssString.toString().length());
bigAssString.replace((1024*1024*3)/4, ((1024*1024*3)/4)+1, "á");
for (String chunk : cutString(bigAssString.toString(), 1024*1024, "UTF-8")) {
System.out.println(String.format(
"Chunk [...] - Chars: %d - Bytes: %d",
chunk.length(), chunk.getBytes("UTF-8").length));
}
/*
Prints:
bigAssString.length = 3145728
Chunk [...] - Chars: 1048575 - Bytes: 1048576
Chunk [...] - Chars: 1048576 - Bytes: 1048576
Chunk [...] - Chars: 1048576 - Bytes: 1048576
Chunk [...] - Chars: 1 - Bytes: 1
*/
关于java - 如何使用 Java 将一个字符串切割成 1 兆字节的子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43499976/
Python 中是否有任何预制的优化工具/库来切割/切片值“小于”某物的列表? 问题来了:假设我有一个列表: a=[1,3,5,7,9] 我想删除所有 2 的项目,将会有很多迭代)。 我也可以使用二进
我创建了一个 fiddle 来更好地展示我想问的问题。 angle crop of pseudo element 这是一张显示所需结果的图片: li.active::after { conte
我有一个字符串如下: 2012/02/01,13:27:20,872226816,-1174749184,2136678400,2138578944,-17809408,2147352576 我想提取
RESTEasy 看起来会忽略尾部斜杠,因此我无法编写带有和不带有尾部斜杠的两个不同的 Web 服务,此示例显示: @Path("foo") public class TestClass {
mov rax,QWORD PTR [rbp-0x10] mov eax,DWORD PTR [rax] add eax,0x1 mov DWORD PTR [rbp-0x14], eax 下
我有以下 xml: External Vendor File External vendor file for so
我在 Python 中工作,并在 Pandas 中使用剪切功能。我想让我的 pd.cut 中的 bins 基于用户定义的逗号分隔整数,具有预定义的上限和下限。换句话说,我想将 bins 预定义为 [0
我正在尝试嵌入 ShareThis将代码写入使用 Bootstrap 创建的网站。 问题是由于某种原因,两个按钮的底部都被切断了,而且我似乎无法使用 Bootstrap 列偏移将按钮居中,因为使用这种
我正在尝试模拟无线传感器节点网络,以研究网络的稳健性。我面临以下问题: 我有一个具有一些边缘容量的节点网络。这相当于算法中的网络流问题。有一个源节点(检测某些事件)和一个接收节点(我的基站)。现在,我
我有一个 UIImageView,带有特定的图像。我还有一个形状奇怪的 UIBezierPath。我想将图像剪切成该形状并返回该形状的新图像。 形式为: func getCut(bezier:UIBe
我有一个大型的全局 .nc 文件数据集,我正试图将它们裁剪到一个较小的区域。我将此区域存储为 .shp 文件。 我曾尝试使用 Qgis 中的 gdal,但需要通过转换每个变量来做到这一点,我必须为所有
我正在使用以下命令来剪切 FLAC 文件:ffmpeg -i input.flac -ss 10s -t 10s -c copy output.flacoutput.flac包含正确的音频持续时间。但
我希望为以下组件制作足迹: AG EMCO HV power supply 现在,这个组件可以通过电路板安装,虽然它不是一个很高的组件,但我希望探索这个选项。我想一个附带问题是:PCB 制造商有多喜欢
我目前正在用 C++ 编写一个小程序(在 Mac 上,其中包含一些 C 代码)并且需要从套接字中检索二进制数据(效果很好),但我还需要切断 HTTP标题。 基本上,我连接到服务器,发送 HTTP GE
我想制作一个Python程序,其中在文本文件中给出DNA序列。它有超过9000个字符。我必须将序列剪成 3 个字符所以我们的框架从1到3读取,然后4到6,然后7到9,这被称为密码子。 例如,序列是 A
我正在开发一个迷你游戏,如果我将 DIN-Ax 作为输入,我需要找出可以在 DIN-A0 纸上画多少行。例如: ;car horizontal and cdr vertical lines (defi
我在下面有这个名为 monitor.log 的报告文件 switch#sh mac address-table int g1/0/1 Mac Address Table -----
我的设备上有 MPEG-TS 文件。我想从设备上的文件开始处截断一个相当准确的时间。 使用 FFmpegWrapper作为基地,我希望能够实现这一目标。 不过,我对 ffmpeg 的 C API 有点
我导出了两个字段:name和header从数据库中使用: SELECT name, header INTO OUTFILE '/var/lib/mysql-files/myfile.txt' FIEL
这个问题在这里已经有了答案: How do you parse and process HTML/XML in PHP? (31 个答案) 关闭 9 年前。 我尝试转换以下字符串 st
我是一名优秀的程序员,十分优秀!