Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.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_Encryption - Fatal编程技术网

Java 在字符串字符之间添加空格

Java 在字符串字符之间添加空格,java,encryption,Java,Encryption,我正在做一个数据加密程序。我已经完成了加密部分,但解密部分有点棘手。在我的程序中,我将纯文本转换为如下内容: public String cipher_stream = new String("2057216345135157862323621293695559030383785129357667531585938249534612483932237388388135819035165204"); 上面的字符串实际上是纯文本的加密形式 clearText = "hello this is br

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

public String cipher_stream = new String("2057216345135157862323621293695559030383785129357667531585938249534612483932237388388135819035165204");
上面的字符串实际上是纯文本的加密形式

clearText = "hello this is bravo.";
现在我想以一种特定的方式向字符串
cipher\u流添加空格。也就是说,第一个空格应插入2位数之后,第二个空格应插入3位数之后,然后再次在2位数之后插入空格,然后再次在3位数之后插入空格。。
在特定模式中插入空格后,字符串
cipher\u流
将如下所示

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);

我很努力,但不知道怎么做。。。如果你们中有人能帮助我,请告诉我。谢谢你的帮助这里有一些伪代码

    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;
     }
    }
boolean two=true;
而(i

你仍然需要做一个方法来插入空格,或者你可以直接用手把它塞进去。希望这有帮助。

写了一些非常快的东西:

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);
    }
}
公共类测试{
公共静态字符串buildNewString(字符串输入){
字符串输出=”;
字符串temp=“”;
int numbersToAppend=2;//这将控制要追加多少个数字
对于(int i=0;i
样本输出:

运行:

旧版本:2057216345135157862236212936955590303837851293576675315859388249534612483932237388388135819035165204
新增:20572 16345 13515 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
,您可能会做得更好,我只是写了这个来展示一些逻辑。我没有任何逻辑来检查我的
子字符串创建时是否超出范围。你可能应该补充一点,因为我假设不是所有的数字都能像给出的例子那样完美地工作。干杯

此方法可以:

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();
}
private静态字符串addSpaces(CharSequence输入){
StringBuilder buf=新的StringBuilder(input.length()*7/5+1);
对于(int i=0;i
请参阅。

我编写了以下代码:

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);
    }
}
输出:

20572163451351578622362129369555903038378512935766753158593882495346124832237388388135819035165204

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


我建议使用一个字符串生成器并继续附加到它,然后将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();
StringBuilder sb=新建StringBuilder();
int k=2;
布尔开关=真;
int i=0;
while(i0)
{
sb.append(cipher_stream.charAt(i));
k=k-1;
i=i+1;
}
else如果(k==0)
{
某人加上(“”);
开关=!开关;
k=k-1;
i=i+1;
}
其他的
{
如果(开关==true)
k=2;
其他的
k=3;
}
}
字符串输出=sb.toString();
字符串密码\u stream=新字符串(“20572163451351578622362129369555903038378512935766753158593882495346124832237388388135819035165204”);
StringBuffer resultbuff=新的StringBuffer(“”);

对于(int pl=0;pl只需构建新字符串(最好使用
StringBuilder
)。“205…204”已经是
字符串,并且是不可变的。无需调用
新字符串()
。为了解释我是什么,它是字符串中的索引。在定义方法时,应该始终指定public、private或protected。此外,如果引用类的非显式公共方法,Java编译器会向您发出警告。代码中有一个bug,您正在用空白spa替换第2和第3个数字ce。从查看
20572
如何变成
20721
就可以看出,它本应该是
20572
。另外,最后3个数字
204
也不见了。@CEELO现在都修好了!感谢您找到它。
  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);