简体   繁体   English

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

[英]Adding spaces in between String characters

I am working on a data encryption program. 我正在研究数据加密程序。 I have completed the encryption part but the decryption part is a bit tricky. 我已经完成了加密部分,但是解密部分有些棘手。 In my program I convert plain text to something like this: 在我的程序中,我将纯文本转换为如下形式:

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

the above String is actually the encrypted form of the plain text 上面的字符串实际上是纯文本的加密形式

clearText = "hello this is bravo.";

now I want to add spaces to the String cipher_stream in a specific way. 现在,我想以特定方式向String cipher_stream添加空格。 that is the first space should be inserted after 2 figure number then the second space should be inserted after three figure number then again a space should be inserted after 2 figure number and then again after three figure number.. the String cipher_stream will look like this after inserting spaces in a specific pattern 也就是说,第一个空格应插入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);

I tried very hard but cannot figure out how to do it... please if any one of you can help me. 我非常努力,但不知道该怎么做...如果有任何人可以帮助我,请。 Thanks for helping 感谢您的帮助

Here's some pseudo code 这是一些伪代码

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

You'll still have to make a method to insert the space or you could just hand jam it in there. 您仍然必须要有一种方法来插入空格,否则就可以用手将其卡在其中。 Hope this helps. 希望这可以帮助。

Wrote something up really quick: 很快就写了一些东西:

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

Sample output: 样本输出:

run: 跑:

Old: 2057216345135157862323621293695559030383785129357667531585938249534612483932237388388135819035165204 旧:2057216345135157862323621293695559030383785129357667531585938249534612483932237388388135819035165204

New: 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 新增:20572 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

BUILD SUCCESSFUL (total time: 0 seconds) 建立成功(总时间:0秒)

You could probably make it better by using Stringbuilder , I just wrote that up to show some logic. 通过使用Stringbuilder ,您可能会Stringbuilder ,我只是在编写代码以显示一些逻辑。 I don't have any logic to check if my substring will be out of bounds when I create it. 创建substring时,我没有任何逻辑可以检查substring是否超出范围。 You should probably add that since I assume not all numbers will work out perfectly like the example given. 您可能应该补充一点,因为我认为并非所有数字都可以像给出的示例那样完美地工作。 Cheers! 干杯!

This method will do it: 这种方法可以做到:

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

See IDEONE demo . 请参阅IDEONE演示

I wrote this code: 我写了这段代码:

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

Output: 输出:

2057216345135157862323621293695559030383785129357667531585938249534612483932237388388135819035165204 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 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

I would suggest take a string builder and keep appending to it and convert StringBuilder back to spring, this would save memory. 我建议使用一个字符串生成器并继续附加它,并将StringBuilder转换回spring,这样可以节省内存。 Something like 就像是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM