将字符串分成两半 [英] Splitting a string into two halfs

查看:219
本文介绍了将字符串分成两半的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

    }
}

推荐答案

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

这篇关于将字符串分成两半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆