java - 将字符串分成两半

标签 java arrays string split binary

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

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.

    }
}

最佳答案

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

关于java - 将字符串分成两半,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32423346/

相关文章:

android - 将包含 ID 的字符串转换为整数 ID

c++ - 在 C++ 中将字符串转换为 int 的问题

java - 我将一个txt文件读取到Java中,我想将其编码为边,其中每条边都是一对节点(DFS)

java - Spring JPA - 连接非持久字段

php - 从数组 : for loop not working 返回字母

javascript - 打印 100 到 200,除了三个异常(exception)?

javascript - 合并和重新排序两个对象数组的问题

java - 查找数组中不存在空值的索引

java - Spring 5 Webclient在doAfterSuccessOrError中抛出异常

c - snprintf 困惑