java - 如何同时去掉两个字符串相同的地方?
巴扎黑
巴扎黑 2017-04-17 17:58:09
0
6
377

比如我现在有

String a = "今天天气很好我叫小王";
String b = "今天天气很好我叫大吴";

运算后想得到

String a = "小王";
String b = "大吴";
巴扎黑
巴扎黑

reply all(6)
迷茫
public static void main(String[] args) throws UnsupportedEncodingException {

        String str = "今天气很好我叫小王";
        String str1 = "今天天气很好我叫大吴";
        String gy = "";
        String encode1 = convert(str);
        String encode2 = convert(str1);
        String[] split = encode1.replace("\", "-").split("-");
        for (int i = 0; i < split.length; i++) {
            String s = split[i];
            if (s != null && !"".equals(s)) {
                if (encode2.indexOf("\" + split[i]) != -1) {
                    gy += "\" + split[i];
                }
            }
        }
        System.out.println("公约字符串-----" + gy);
        System.out.println(decodeUnicode(gy));
    }

    public static String convert(String str) {
        str = (str == null ? "" : str);
        String tmp;
        StringBuffer sb = new StringBuffer(1000);
        char c;
        int i, j;
        sb.setLength(0);
        for (i = 0; i < str.length(); i++) {
            c = str.charAt(i);
            sb.append("\u");
            j = (c >>> 8); // 取出高8位
            tmp = Integer.toHexString(j);
            if (tmp.length() == 1)
                sb.append("0");
            sb.append(tmp);
            j = (c & 0xFF); // 取出低8位
            tmp = Integer.toHexString(j);
            if (tmp.length() == 1)
                sb.append("0");
            sb.append(tmp);

        }
        return (new String(sb));
    }

    public static String decodeUnicode(String theString) {
        char aChar;
        int len = theString.length();
        StringBuffer outBuffer = new StringBuffer(len);
        for (int x = 0; x < len;) {
            aChar = theString.charAt(x++);
            if (aChar == '\') {
                aChar = theString.charAt(x++);
                if (aChar == 'u') {
                    // Read the xxxx
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = theString.charAt(x++);
                        switch (aChar) {
                        case '0':
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                        case '8':
                        case '9':
                            value = (value << 4) + aChar - '0';
                            break;
                        case 'a':
                        case 'b':
                        case 'c':
                        case 'd':
                        case 'e':
                        case 'f':
                            value = (value << 4) + 10 + aChar - 'a';
                            break;
                        case 'A':
                        case 'B':
                        case 'C':
                        case 'D':
                        case 'E':
                        case 'F':
                            value = (value << 4) + 10 + aChar - 'A';
                            break;
                        default:
                            throw new IllegalArgumentException(
                                    "Malformed   \uxxxx   encoding.");
                        }
                    }
                    outBuffer.append((char) value);
                } else {
                    if (aChar == 't')
                        aChar = '\t';
                    else if (aChar == 'r')
                        aChar = '\r';
                    else if (aChar == 'n')
                        aChar = '\n';
                    else if (aChar == 'f')
                        aChar = '\f';
                    outBuffer.append(aChar);
                }
            } else
                outBuffer.append(aChar);
        }
        return outBuffer.toString();
    }
小葫芦
let a = "今天天气很好我叫小王";
let b = "今天天气很好我叫大吴";

[a, b] = (a + b).replace(/(.+)(.+)/, '\n').split('\n');

console.log(a, b);

I’m here to join in the fun, es6 means no pressure. . .

Ty80

Pattern matching problem

左手右手慢动作

If it’s like you said:

It’s best to remove the same string of characters that are the same at the front

It can be like this:

    String a = "今天天气很好我叫小王";
    String b = "今天天气很好我叫大吴";

    int length = Math.min(a.length(), b.length());
    int pos = 0;
    while (pos < length) {
        if (0 != (a.charAt(pos) ^ b.charAt(pos))) {
            break;
        }
        pos++;
    }
    System.out.println(a.substring(pos));
    System.out.println(b.substring(pos));

To meet greater needs, it may need to be further spread out

刘奇

Try this idea. Traverse the first string, and after getting the characters, determine whether the second string contains it. If it does, remove all the characters in the two strings.

Peter_Zhu

Actually, the requirements can be explained a little more clearly. If the two strings are "The weather is very good today and my name is Xiao Wang" and "The weather is very good today and my name is Da Wu and he is Xiao Wang's brother", what should be obtained in this case?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template