简体   繁体   中英

Splitting a string into two halfs

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

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

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

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

PS Note that for Strings with odd number of characters, the second-half String will get the benefit of the extra character.

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 main function, call the method as below:

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

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 String with odd length, the second half will be one character longer.

This method drops the middle character of the Strings having odd lengths. This is done by the 2 + 1 in the second half in the else part.

public void method()
{
    Scanner sc = new Scanner(System.in);
    System.out.println("ENTER A STRING");
    original = sc.next();
    
    if(original.length()%2 == 0)
    {
        firsthalf =original.substring(0, original.length()/2);
        secondhalf = original.substring(original.length()/2);
        System.out.println(firsthalf);
        System.out.println(secondhalf);
    }
    else
    {
        firsthalf = original.substring(0, original.length()/2);
        secondhalf = original.substring(original.length()/2+1);
        System.out.println(firsthalf);
        System.out.println(secondhalf);
    }   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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