在String字符之间添加空格

时间:2016-03-01 01:26:20

标签: java encryption

我正在研究数据加密程序。我已经完成了加密部分,但解密部分有点棘手。在我的程序中,我将纯文本转换为这样的内容:

public String cipher_stream = new String("2057216345135157862323621293695559030383785129357667531585938249534612483932237388388135819035165204");

上面的String实际上是纯文本的加密形式

clearText = "hello this is bravo.";

现在我想以特定的方式向字符串cipher_stream添加空格。那是第一个空格应该在2个图号之后插入然后第二个空格应该插入三个数字后再插入一个空格应该插入2个数字后再插入三个数字后面。 在以特定模式插入空格后,字符串cipher_stream将如下所示

public String cipher_code = new String(20 572 16 345 13 515 78 623 23 621 29 369 55 590 30 383 78 512 93 576 67 531 58 593 82 495 34 612 48 393 22 373 88 388 13 581 90 351 65 204);

我努力尝试但无法弄明白该怎么做...请你们中的任何一个人帮助我。谢谢你的帮助

6 个答案:

答案 0 :(得分:0)

这里有一些伪代码

    boolean two = true;
    while (i < string.length())
    {
     if (two)
     {
     i = i + 2;
     string.insertSpace(i)
     two = false
     }
     else
     {
     i = i + 3;
     string.insertSpace(i)
     two = true;
     }
    }

您仍然需要制作插入空间的方法,否则您可以将其卡在那里。希望这会有所帮助。

答案 1 :(得分:0)

快速写出来的东西:

public class Test {

  public static String buildNewString(String input) {
        String output = "";
        String temp = "";
        int numbersToAppend = 2; //This will control how many numbers to append
        for (int i = 0; i < input.length(); i++) {
            if (numbersToAppend == 2) {
                temp = input.substring(i, i + numbersToAppend) + " ";
                output += temp;
                i += numbersToAppend - 1; //Skip the next 2
                numbersToAppend = 3; //Update to append 3 next time
            } else { //Must be 3
                temp = input.substring(i, i + numbersToAppend) + " ";
                output += temp;
                i += numbersToAppend - 1; //Skip the next 3
                numbersToAppend = 2; //Update to append 2 next time
            }
        }
        return output;
    }

    public static void main(String[] args) throws Exception {
        String cipher_stream = new String("2057216345135157862323621293695559030383785129357667531585938249534612483932237388388135819035165204");
        System.out.println("Old: " + cipher_stream);
        String newString = buildNewString(cipher_stream);
        System.out.println("New: " + newString);
    }
}

示例输出:

运行:

  

旧:2057216345135157862323621293695559030383785129357667531585938249534612483932237388388135819035165204

  新:20 572 16 345 13 515 78 623 23 621 29 369 55 590 30 383 78 512 93 576 67 531 58 593 82 495 34 612 48 393 22 373 88 388 13 581 90 351 65 204


  建立成功(总时间:0秒)

你可以通过使用Stringbuilder来改善它,我只是写了一些来展示一些逻辑。在创建substring时,我没有任何逻辑来检查我的1|$item|$price|$quality|$keywords 是否会超出范围。你可能应该补充一点,因为我认为不是所有的数字都会像给出的例子一样完美。干杯!

答案 2 :(得分:0)

这种方法可以做到:

private static String addSpaces(CharSequence input) {
    StringBuilder buf = new StringBuilder(input.length() * 7 / 5 + 1);
    for (int i = 0; i < input.length(); i++) {
        if (i != 0 && (i % 5 == 2 || i % 5 == 0))
            buf.append(' ');
        buf.append(input.charAt(i));
    }
    return buf.toString();
}

请参阅IDEONE demo

答案 3 :(得分:0)

我写了这段代码:

public class C {
    public static void main(String[] args) {
        String cipher_stream = "2057216345135157862323621293695559030383785129357667531585938249534612483932237388388135819035165204";
        System.out.println(cipher_stream);
        boolean twoOrThree = false; // false for 2, true for 3
        StringBuilder sb = new StringBuilder();
        String result = "";

        for (char c : cipher_stream.toCharArray()) {
            if (sb.length() == 0 || (sb.length() == 1 && twoOrThree))
                sb.append(c);
            else {
                sb.append(" ");
                sb.append(c);
                result += sb.toString();
                sb.setLength(0); // Clears the StringBuilder
                twoOrThree = twoOrThree ? false : true;
            }
        }
        System.out.println(result);
    }
}

输出:

  

2057216345135157862323621293695559030383785129357667531585938249534612483932237388388135819035165204

     

20 721 34 135 57 623 36 129 69 559 30 837 51 935 66 531 85 382 95 461 48 932 37 883 81 581 03 165

答案 4 :(得分:0)

我建议带一个字符串生成器并继续追加它并将StringBuilder转换回spring,这样可以节省内存。像

这样的东西
StringBuilder sb = new StringBuilder();
int k = 2;
boolean switch = true;
int i = 0;
while(i < cipher_stream.length)
{
      if(k > 0)
      {
          sb.append(cipher_stream.charAt(i));
          k=k-1;
          i=i+1;
      }
      else if(k == 0)
      {
          sb.append(" ");
          switch = !switch;
          k=k-1;
          i = i+1;
      }
      else
      {
          if(switch == true)
                k = 2;
          else
                k = 3;
      }
}

String output = sb.toString();

答案 5 :(得分:0)

  String cipher_stream = new String("2057216345135157862323621293695559030383785129357667531585938249534612483932237388388135819035165204");
  StringBuffer resultbuff=new StringBuffer("");
  for(int pl = 0;pl<cipher_stream.length();pl++)
  {
            //append space as per given condition...
            if (pl!= 0 && (pl % 5 == 2 || pl % 5 == 0))
                resultbuff.append(' ');
               resultbuff.append(cipher_stream.charAt(pl));


  }
  System.out.println(resultbuff);