如何将一个字符串分割成多个相等大小的子字符串

huangapple go评论71阅读模式
英文:

How to split a string into several strings of equal sizes

问题

我需要将一个二进制数转换为十六进制数。我决定这样做的方法是将二进制字符串分成若干长度为4的子字符串,并为每个子字符串赋予其在十六进制数中对应的值(例如,1000 = 8,1101 = D)。

我看到有几个问题询问如何将字符串分成长度为4的子字符串,但所有这些解决方案都使用了一个正则表达式,导致得到一个单独的字符串。例如,我在一个解决方案中找到了这行代码:

System.out.println(Arrays.toString("String".split("(?<=\\G.{4})")));

当我尝试将其用于二进制数"10011000"时,我得到了"[1001, 1000]",但作为一个单独的字符串(括号、逗号和空格都包含在字符中),我仍然面临同样的问题,如何将一个字符串分割。

有没有一种将字符串拆分为较小字符串数组的方法?

英文:

I have to convert a binary number to a hex number. The way I have decided to do this is to split the binary string into several strings of length 4 and assign each string its corresponding value in hex number (i.e. 1000 = 8, 1101 = D).

I have seen several question asking for a way to split a string into strings of size 4 the same thing but all of those solutions used a regex that gave a single string. For example I found this line of code in a solution:

System.out.println(Arrays.toString("String".split("(?<=\G.{4})")));

When I tried to use it with the binary number "10011000", I got "[1001, 1000]" but as a single string (the brackets, comma, and blank space were included as characters) and I was left with the same problem, how do I split a string.

Is there a way to split a string into an array of smaller strings?

答案1

得分: 1

你可以尝试将字符串转换为字符数组,然后再转换为另一个字符串数组,在字符数组的基础上每4个字符添加到新的字符串数组中。

尝试这样做:

String BinaryNumber = "10011010";

char[] n = new char[BinaryNumber.length()];

for(int i=0; i<BinaryNumber.length(); i++){
    n[i] = BinaryNumber.charAt(i);
}

String str;
String[] NumberArray = new String[(BinaryNumber.length())/4];
int count = 0;

for(int i=0; i<BinaryNumber.length(); i+=4){

    str = String.valueOf(n[i])+String.valueOf(n[i+1])+String.valueOf(n[i+2])+String.valueOf(n[i+3]);
    NumberArray[count] = str;
    count++;

}

我认为这可能是解决方案,但只有在BinaryNumber的长度能够被4整除时才有效。

英文:

You can try making the string a char array and then into another array of strings, add each 4 characters of the char array.

Try this:

    String BinaryNumber = &quot;10011010&quot;;
    
    char[] n = new char[BinaryNumber.length()];
    
    for(int i=0; i&lt;BinaryNumber.length(); i++){
        n[i] = BinaryNumber.charAt(i);
    }
    
    String str;
    String[] NumberArray = new String[(BinaryNumber.length())/4];
    int count = 0;
    
    for(int i=0; i&lt;BinaryNumber.length(); i+=4){
        
        str = String.valueOf(n[i])+String.valueOf(n[i+1])+String.valueOf(n[i+2])+String.valueOf(n[i+3]);
        NumberArray[count] = str;
        count++;
        
    }

I think this might be the solution, though it will only work if the length of the BinaryNumber is divisible by 4.

答案2

得分: 1

尝试这样做:

String binaryNumber = "110101111";
// 首先确保二进制字符串的长度是4的倍数,如果不是,在左边用0位进行填充。

binaryNumber = "0".repeat(3 - (binaryNumber.length()+3) % 4)
        + binaryNumber;

// 然后按照你所描述的方式分割字符串。
String[] groups = binaryNumber.split("(?<=\\G.{4})");

for (String v : groups) {
    System.out.println(v);
}

输出结果为

0001
1010
1111
英文:

Try it like this.

String binaryNumber = &quot;110101111&quot;;
// first make certain the binary string is a multiple of length four so
// pad on the left with 0 bits.

binaryNumber = &quot;0&quot;.repeat(3 - (binaryNumber.length()+3) % 4)
		+ binaryNumber;

// Then you can just split it like this as you described.
String[] groups = binaryNumber.split(&quot;(?&lt;=\\G.{4})&quot;);

for (String v : groups) {
	System.out.println(v);
}

prints

0001
1010
1111


</details>



huangapple
  • 本文由 发表于 2020年9月15日 06:38:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63892654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定