Java 将一个字符串分成两半

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32423346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:31:00  来源:igfitidea点击:

Splitting a string into two halfs

javaarraysstringsplitbinary

提问by

I am making a new conversion software to hide messages (for fun). I have made a Binary and Decimal conversion class and my idea is, a user inputs string, it converts all to Binary format. Then splits it in half, converts one half to decimal, then adds the string back together again to make it a mix of binary and decimal. In other versions I will add more conversions, more splits,and maybe convert to languages too. I need splitting the string in half. This is my code so far::

我正在制作一个新的转换软件来隐藏消息(为了好玩)。我做了一个二进制和十进制转换类,我的想法是,用户输入字符串,它将所有转换为二进制格式。然后将其分成两半,将一半转换为十进制,然后再次将字符串相加,使其成为二进制和十进制的混合。在其他版本中,我将添加更多转换、更多拆分,并且也可能转换为语言。我需要将字符串分成两半。到目前为止,这是我的代码::

public ray() {
    Scanner in = new Scanner(System.in);

    while (true) {
        System.out.println("Please input the text you wish to encode!");
        System.out.println("Type '#' to quit the Software.");
        String s1 = in.nextLine();
        if ("#".equalsIgnoreCase(s1)) {
            System.out.println("Goodbye, hope you enjoyed RAYConversion v1.0 Alpha.");
            // close software
        }

        // convert all to binary
        binary Binary = new binary();

        //what i need to do is split stirng s1 in half, make it into different strings. Then I will convert
        //the two strings to binary and decimal. I made a converter for that.

    }
}

采纳答案by karim mohsen

final int mid = s1.length() / 2; //get the middle of the String
String[] parts = {s1.substring(0, mid),s1.substring(mid)};
System.out.println(parts[0]); //first part
System.out.println(parts[1]); //second part

回答by burglarhobbit

I guess this will get you done what you want to achieve:

我想这会让你完成你想要实现的目标:

P.S. Note that for Strings with oddnumber of characters, the second-half String will get the benefit of the extra character.

PS请注意,对于具有odd字符数的字符串,后半字符串将获得额外字符的好处。

Create a method like this:

创建一个这样的方法:

public static String substring(int a, int b, String temp) {
    String a = "";
    for(int i = a; i<b; i++) {
        char ch1 = temp.charAt(i);
        a = a + ch1;
    }
    return a;
}

and in your mainfunction, call the method as below:

并在您的main函数中,调用如下方法:

String s1a = substring(0, (s1.length()/2), s1);
String s1b = substring((s1.length()/2),s1.length(), s1);

回答by Paras Diwan

You can use Java's substring function to divide them in halves:

您可以使用 Java 的 substring 函数将它们分成两半:

String s1a = s1.substring(0, (s1.length()/2));
String s1b = s1.substring((s1.length()/2);

回答by bcsb1001

Try this:

尝试这个:

int len = whole.length();
String a = whole.substring(0, len / 2), b = whole.substring(len / 2);

This uses the substring()method. In the case of a Stringwith odd length, the second half will be one character longer.

这是使用substring()方法。在String长度为奇数的情况下,后半部分将长一个字符。