Home » Java » java – How to change chars between 2 strings?-Exceptionshub

java – How to change chars between 2 strings?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to change a char in a String with another in second String,the distance is 3.For example String s1:abc,String s2:abcdef->s2 will be abadef then -> abadeb.If it is possible all chars from first String should goes to second s2.I have to mention that upperCase letters from the first should be lowerCase.And only letters will be taken from the first String.I know Strings are immutable

public static void Encrypt(String s1, String s2) {
        s1 = s1.toLowerCase();
        StringBuilder finalS2 = new StringBuilder();
        finalS2.append(s2);

        for (int i = 0; i < finalS2.length(); i++) {

            if (s1.charAt(i) >= 'a' && s1.charAt(i) <= 'z') {

                finalS2.setCharAt(i + 2, s1.charAt(0));

                break;
            }
        }

        System.out.println(finalS2);
    }

I think in my method,wich i don’t think it’s a good idea,i should place another for loop for s1 and a break somewhere.I tried many methods,with charArray,etc but didn’t managed.

How to&Answers:

If I’ve understood correctly this should work. everyX in your example would be 3

public static String encrypt(String s1, String s2, int everyX) {
    //Start with S2
    StringBuilder finalS2 = new StringBuilder(s2);
    s1 = s1.toLowerCase();

    int indexInS1 = 0;
    //Whilst there are characters in S2 and S1 left to iterate over
    for (int placeInS2 = 0; placeInS2 < finalS2.length() && indexInS1 < s1.length(); placeInS2++) {
         //Has there been everyX places encountered and the character in S1 is alphanumeric
        if ((placeInS2 + 1) % everyX == 0) {
            if (Character.isAlphabetic(s1.charAt(indexInS1)) {
                finalS2.setCharAt(placeInS2, s1.charAt(indexInS1));
            }
            indexInS1++;
        }
    }

    return finalS2.toString();
}

Answer:

So you want to take the original text of s2 and replace every third character with characters from s1:

s1: abc
    ││└───── ignored
    │└───┐
    └─┐  │
s2: abcdef

Best way to do incremental single-char replacements like that is to get the char[] of the original string (s2), replace the appropriate characters, then build the resultant string from that.

public static String encrypt(String s1, String s2) {
    char[] buf = s2.toCharArray();
    for (int i = 0, j = 2; i < s1.length() && j < buf.length; i++, j += 3) {
        buf[j] = Character.toLowerCase(s1.charAt(i));
    }
    return new String(buf);
}

Test

System.out.println(encrypt("abc", "abcdef"));

Output

abadeb

If you want the code to handle characters from the Unicode supplemental planes, e.g. emojis, then you need to work with code points.

You also mentioned that you only want letters from s1, so we’ll add that to the next version (codePoints() requires Java 9+).

public static String encrypt(String s1, String s2) {
    int[] c1 = s1.codePoints().toArray();
    int[] c2 = s2.codePoints().toArray();
    for (int i = 0, j = 2; i < c1.length && j < c2.length; i++) {
        if (Character.isLetter(c1[i])) {
            c2[j] = Character.toLowerCase(c1[i]);
            j += 3;
        }
    }
    return new String(c2, 0, c2.length);
}

Test

System.out.println(encrypt("abc", "abcdef"));
System.out.println(encrypt("a😈c👿e", "a😀c😃e😄g😁i😆k😅m🤣"));

Output

abadeb
a😀a😃ecg😁e😆k😅m🤣

As you can see, emoji characters in s1 are skipped, since they are not “letters”, and emojis in s2 are correctly replaced. Even though 😄 takes up 2 char positions in s2, it is correct replaced by c, a single char value.