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

发布于 2024-12-29 06:26:55 字数 1096 浏览 1 评论 0原文

我正在创建一个简单的程序来将二进制数转换为十六进制数,而不使用 Java 提供的方法来执行此操作。我已经有了大部分代码,只是我的“分割字符串”方法遇到了麻烦。

基本上我在这个方法中想做的是

1) 接受一个字符串(二进制数)

2) 创建一个字符串数组来保存数字的“分组”

3) 将二进制数分成 4 位数字的组(例如 10011101 = 1001, 1101)

4) 返回分组数组

但是,当使用我的方法时,“分组”数组中的第一个元素始终只需要 3。 (例如应该是“1001”,但只输入“100”)。我在这里做错了什么?

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

I am creating a simple program to convert binary numbers to hex, without the use of the methods provided by Java to do so. 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)

2) Create an array of strings to hold the "groupings" of numbers

3) Split the binary number into groups of 4 digits (e.g. 10011101 = 1001, 1101)

4) Return the array of groupings

However, when using my method, it always only takes 3 for the first element in my "groupings" array. (e.g. should be "1001", but only putting "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;   
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猛虎独行 2025-01-05 06:26:55

请记住,子字符串不会返回由 end 表示的索引处的字符。它返回 end-1。

Javadoc 并提取

public String substring(int beginIndex,
int endIndex)

返回一个新字符串,该字符串是该字符串的子字符串。子字符串从指定的 beginIndex 开始,一直延伸到索引 endIndex - 1 处的字符。因此,子字符串的长度为 endIndex-beginIndex。

示例:

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

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

Remember that substring will not return the character at the index denoted by end. It returns end-1.

The Javadoc and extract

public String substring(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.

Examples:

"hamburger".substring(4, 8) returns "urge"

"smiles".substring(1, 5) returns "mile"

花开浅夏 2025-01-05 06:26:55

String.substring 是独占的,请仔细查看文档中的示例。

The second parameter (endIndex) in String.substring is exclusive, look closely at the examples in the doc.

稀香 2025-01-05 06:26:55

复制此代码并运行它,它可以正常工作。所以没有问题。使用可整除 4 的 String.length 的输出是正确的。

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.

夜清冷一曲。 2025-01-05 06:26:55

您的方法工作正常,尽管当必须拆分的数字的大小不是四的倍数时它会抛出错误。这个方法更通用一些:

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;

    }

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;

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文