search

Home  >  Q&A  >  body text

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

比如我现在有

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

运算后想得到

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

reply all(6)I'll reply

  • 迷茫

    迷茫2017-04-17 18:00:09

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

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 18:00:09

    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. . .

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 18:00:09

    Pattern matching problem

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 18:00:09

    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

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 18:00:09

    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.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 18:00:09

    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?

    reply
    0
  • Cancelreply