简体   繁体   English

.subString方法无法正常工作,Java

[英].subString method not working correctly, Java

I am creating a simple program to convert binary numbers to hex, without the use of the methods provided by Java to do so. 我正在创建一个简单的程序,将二进制数转换为十六进制,而无需使用Java提供的方法。 I have most of my code already, I'm just having trouble with my "split string" method. 我已经有了大部分代码,但是我的“分割字符串”方法遇到了麻烦。

Basically all I am trying to do in this method is 基本上我要用这种方法做的就是

1) Take in a string (binary number) 1)输入一个字符串(二进制数)

2) Create an array of strings to hold the "groupings" of numbers 2)创建一个字符串数组来保存数字的“分组”

3) Split the binary number into groups of 4 digits (eg 10011101 = 1001, 1101) 3)将二进制数分成4位一组(例如10011101 = 1001,1101)

4) Return the array of groupings 4)返回分组数组

However, when using my method, it always only takes 3 for the first element in my "groupings" array. 但是,使用我的方法时,“分组”数组中的第一个元素始终只需要3。 (eg should be "1001", but only putting "100"). (例如应为“ 1001”,但只能放入“ 100”)。 What am I doing wrong here? 我在这里做错了什么?

public String[] splitIntoFours (String toSplit) {
    int stringPart = 4;
    int arraySize = toSplit.length() / 4;
        String[] groupings = new String[arraySize];
        for (int iterator = 0; (iterator * stringPart) < toSplit.length(); iterator++){
            //If statement to deal with the inital case of the iterator being 0, 
            //where this algorithm only takes the first 3 numbers instead of a 
            //sequence of 4 numbers.

            int start = iterator * stringPart;
            int end = start + stringPart;
            if (end > toSplit.length()) {
                 end = toSplit.length();
            }
            groupings[iterator] = toSplit.substring(start, end);
        }
    return groupings;   
    }

Remember that substring will not return the character at the index denoted by end. 请记住,子字符串不会返回以end表示的索引处的字符。 It returns end-1. 它返回end-1。

The Javadoc and extract Javadoc和摘录

public String substring(int beginIndex, int endIndex) 公共字符串子字符串(int beginIndex,int endIndex)

Returns a new string that is a substring of this string. 返回一个新字符串,该字符串是该字符串的子字符串。 The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. 子字符串从指定的beginIndex开始,并扩展到索引endIndex-1处的字符。因此,子字符串的长度为endIndex-beginIndex。

Examples: 例子:

"hamburger".substring(4, 8) returns "urge" “ hamburger” .substring(4,8)返回“ urge”

"smiles".substring(1, 5) returns "mile" “ smiles” .substring(1,5)返回“ mile”

String.substring中的第二个参数(endIndex)是互斥的,请仔细阅读文档中的示例。

Copying this code and running it, it works correctly. 复制此代码并运行它,即可正常工作。 So there is no problem. 因此没有问题。 The output, using a String.length dividable through 4, is correct. 使用可细分为4的String.length的输出是正确的。

Your method works fine, although it throws an error when the size of the number that has to be split is not an multiple of four. 您的方法运行良好,尽管当必须拆分的数字大小不是四的倍数时会引发错误。 This method is a bit more generic: 此方法更通用:

public static String[] split(String str, int sizeOfGroups) {
        int arraySize = str.length() / sizeOfGroups;
        String[] groupings = new String[arraySize+1];
        int num1, num2;

        for (int i = 0; i * sizeOfGroups < str.length(); i++) {

            num1 = i * sizeOfGroups + sizeOfGroups;
            num2 = i * sizeOfGroups;

            if ((num1 >= str.length())) {
                groupings[i] = str.substring(num2, str.length());
            } else {
                groupings[i] = str.substring(num2, num1);
            }
        }

        return groupings;

    }

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

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