首页 > 如何同时去掉两个字符串相同的地方?

如何同时去掉两个字符串相同的地方?

比如我现在有

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

运算后想得到

String a = "小王";
String b = "大吴";

其实需求还能再说清楚一点,如果两个串分别是“今天天气很好我叫小王”和“今天天气很好我叫大吴是小王他哥”,这种情况要得到什么


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

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

console.log(a, b);

我是来凑热闹的,,es6 表示无压力。。。


如果像你说的:

只要最前面连续的相同字符串去掉最好

可以这样:

    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));

要满足更大的需求,可能需要再发散一下


模式匹配问题


试试这个思路。遍历第一个串,得到字符后判断第二个串是否包含,如果包含,把两个串里所有的这个字符都去掉。


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();
    }
【热门文章】
【热门文章】