Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-从字节数组中修剪尾随空格_Java - Fatal编程技术网

Java-从字节数组中修剪尾随空格

Java-从字节数组中修剪尾随空格,java,Java,我有类似的字节数组: [77, 83, 65, 80, 79, 67, 32, 32, 32, 32, 32, 32, 32] 大致相当于 [M , S, A, P, O, C, , , , , , , ] when printed as chars. 现在我想修剪尾随的空格,使其看起来像: [77, 83, 65, 80, 79, 67] 最简单的方法是什么 编辑:我不想处理字符串,因为存在不可打印字节的可能性,我无法承受丢失这些数据的后果。它需要字节数组:(每当我转换成字

我有类似的字节数组:

[77, 83, 65, 80, 79, 67, 32, 32, 32, 32, 32, 32, 32]
大致相当于

[M , S, A, P, O, C,  ,  ,  ,  ,  ,  ,  ] when printed as chars.
现在我想修剪尾随的空格,使其看起来像:

[77, 83, 65, 80, 79, 67]
最简单的方法是什么

编辑:我不想处理字符串,因为存在不可打印字节的可能性,我无法承受丢失这些数据的后果。它需要字节数组:(每当我转换成字符串时,像01(SOH)02(STX)等字节都会丢失

编辑2只是澄清一下。如果我将字节数组转换为字符串,是否会丢失数据?现在有点困惑。如果字节属于不同的字符集,该怎么办?
  • 将字节更改为字符串
  • call
    text=text.replaceAll(“\\s+$”,“”);//只删除尾随的空格
  • 将字符串更改为字节

    • 最简单的方法?没有效率或性能的保证,但似乎很简单

      byte[] results = new String(yourBytes).trim().getBytes();
      

      不转换为字符串:

      byte[] input = /* whatever */;
      int i = input.length;
      while (i-- > 0 && input[i] == 32) {}
      
      byte[] output = new byte[i+1];
      System.arraycopy(input, 0, output, 0, i+1);
      

      测验:

      修改字符串
      trim()
      用于
      字节[]
      。它不仅切割数组的尾部,还切割数组的头部

      public byte[] trimArray(byte[] source) {
          int len = source.length;
          int st = 0;
          byte[] val = source;
      
          while ((st < len) && (val[st] <= SPACE)) {
              st++;
          }
          while ((st < len) && (val[len - 1] <= SPACE)) {
              len--;
          }
          byte[] result;
          if ((st > 0) || (len < source.length)) {
              result = new byte[len - st];
              System.arraycopy(source, st, result, 0, result.length);
          } else {
              result = source;
          }
      
          return result;
      }
      
      公共字节[]数组(字节[]源){
      int len=source.length;
      int st=0;
      字节[]val=源;
      
      而((sti>=0
      而不是
      i@ctomek,你在技术上是正确的。但是,我(大多数人)遵循谷歌的Java风格指南,其中规定@ctomek是我自己的个人体验。它只是更容易在数月/数年内正确地维护/阅读/更改。例如:我曾经因为一个
      if(…);{}而花了令人沮丧的时间寻找一个bug
      这本不应该有那么一点
      。一旦你遵循了严格、一致的风格,你就可以使用checkstyle之类的工具来强制执行它,并在代码执行之前检测错误。
      public byte[] trimArray(byte[] source) {
          int len = source.length;
          int st = 0;
          byte[] val = source;
      
          while ((st < len) && (val[st] <= SPACE)) {
              st++;
          }
          while ((st < len) && (val[len - 1] <= SPACE)) {
              len--;
          }
          byte[] result;
          if ((st > 0) || (len < source.length)) {
              result = new byte[len - st];
              System.arraycopy(source, st, result, 0, result.length);
          } else {
              result = source;
          }
      
          return result;
      }