Java:从字节数组中删除连续的零段 [英] Java: remove continious segment of zeros from byte array
问题描述
例如,假设我想从数组中删除0个长度大于3个字节的所有连续段
For example, let's say I want to delete from the array all continuous segments of 0's longer than 3 bytes
byte a[] = {1,2,3,0,1,2,3,0,0,0,0,4};
byte r[] = magic(a);
System.out.println(r);
结果
{1,2,3,0,1,2,3,4}
我想做一些像Java中的正则表达式,但是在字节数组而不是字符串上。
I want to do something like a regular expression in Java, but on a byte array instead of a String.
是否有一些东西可以帮助我内置(或者是有一个很好的第三方工具),还是我需要从头开始工作?
Is there something that can help me built-in (or is there a good third party tool), or do I need to work from scratch?
字符串是UTF-16,所以来回转换不是一个好主意?至少它浪费了大量的开销......对吗?
Strings are UTF-16, so converting back and forth isn't a good idea? At least it's a lot of wasted overhead ... right?
推荐答案
正则表达式不是工作的工具,你会改为需要从头开始实施
regex is not the tool for the job, you will instead need to implement that from scratch
这篇关于Java:从字节数组中删除连续的零段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!