简体   繁体   English

逐个字符比较多个字符串

[英]Comparing multiple strings character by character java

I wanted compare multiple strings character by character and want to output a string which contains the characters which are present in most of the string. 我想逐个字符地比较多个字符串,并想输出一个包含大多数字符串中存在的字符的字符串。 For example I used three strings: Input:S1= where, S2= wHere, S3=whera Output:S=where. 例如,我使用了三个字符串:Input:S1 = where,S2 = wHere,S3 = whera Output:S = where。

I could do it for 3 strings using the code : 我可以使用代码对3个字符串进行处理:

public class stringc {

    static String S1="where";
    static String S2="wHere";
    static String S3="whera";
    static StringBuilder S=new StringBuilder();

    public static void main(String[] args)
    {
        for (int i=0;i<5;i++)
        {

            if(S1.charAt(i)==S2.charAt(i)||S1.charAt(i)==S3.charAt(i))
            {
                S.append(S1.charAt(i));
            }
        }
    }
}

Can anybody help me how to use it for more than 10 strings. 任何人都可以帮助我如何将其用于十多个字符串。

使用数组/字符串列表,而不是int i=0;i<5;i++使用int i=0;i<array.length;i++ //将list.size()用于列表

How I'd do it: 我该怎么做:

  1. for each of your strings, call toCharArray() 对于每个字符串,调用toCharArray()
  2. Add the resulting char[] to a 2D char array 将结果char []添加到2D char数组中
  3. For each word in the 2D array, add the character you are inspecting to a map, incrementing a counter if the character is already in the maps keySet() 对于2D数组中的每个单词,将要检查的字符添加到地图中,如果该字符已经在地图中,则增加一个计数器keySet()
  4. The character with the most occurrences (highest counter) in the map is the character to add to the resulting string. 映射中出现次数最多(计数器最高)的字符是要添加到结果字符串中的字符。
  5. Repeat steps 3 and 5 for all characters in the strings 对字符串中的所有字符重复步骤3和5
  6. Output the string you've built 输出您构建的字符串

Here's a block of code using your example with a few more strings: 这是使用您的示例的代码块,其中包含更多字符串:

public static void main(final String[] args) {
    final  StringBuilder stringBuilder = new StringBuilder();

    //Points 1 and 2
    final char[][] characters = new char[][]{
            "where".toCharArray(),
            "wHere".toCharArray(),
            "where".toCharArray(),
            "wperg".toCharArray(),
            "where".toCharArray(),
            "w6ere".toCharArray(),
            "where".toCharArray(),
            "where".toCharArray(),
            "wHere".toCharArray(),
            "w4eeg".toCharArray(),
            "where".toCharArray(),
            "wHare".toCharArray(),
            "where".toCharArray(),
            "where".toCharArray(),
            "weede".toCharArray(),
            "whare".toCharArray(),
            "wHect".toCharArray(),
            "where".toCharArray(),
            "wHere".toCharArray(),
            "whara".toCharArray()
    };

    //Point 3
    for (int i=0; i < 5 ; i++) {

        //Point 4
        final Map<String, Integer> occurrences = new HashMap<String, Integer>();
        for(int j = 0; j < characters.length; j++ ) {

            final String character = ""+characters[j][i];

            if( occurrences.containsKey(character) ) {
                Integer currentTotal = occurrences.get(character);
                occurrences.put(character, ++currentTotal);
                continue;
            }

            occurrences.put(character, 1);
        }

        //Point 5
        int mostOccurrences = 0;
        String characterWithMostOccurrences = "";
        for (final String character : occurrences.keySet()) {
            if( occurrences.get(character) > mostOccurrences ) {
                mostOccurrences = occurrences.get(character);
                characterWithMostOccurrences = character;
            }
        }

        stringBuilder.append(characterWithMostOccurrences);

    }

    //Point 6
    System.out.println(stringBuilder.toString());
}

Here's one I knocked up. 这是我敲的 It compares the characters in the input strings to check if they are the same in every string in the same position. 它比较输入字符串中的字符,以检查它们在相同位置的每个字符串中是否相同。 If the characters in the position match in all input strings, the same character will be in the result string at that position. 如果该位置的字符在所有输入字符串中均匹配,则相同字符将在该位置的结果字符串中。

public class MultipleStringComparator {

public static void main(String[] args) {

    String[] input = new String[] {"abcdeFgh","abcdefgh","abcdefgh","abcdefgh","abcdefgh"};

    MultipleStringComparator comp = new MultipleStringComparator();
    System.out.println(comp.compare(input, '.'));
}

/**
 * Compare strings character by character
 * @param strings
 * @param substitute - character to insert in result where strings do not match
 * @return result
 */
public String compare(String[] strings, char substitute) {

    //How long is the longest string?
    int longest = 0;
    for (String string : strings) {
        if (string.length()>longest) longest = string.length();
    }

    //Initialise result
    StringBuffer result = new StringBuffer();

    //compare strings character by character
    //If the corresponding characters in all input strings match, append to result
    for (int position=0; position<longest; position++) {
        char character = allCharactersMatch(strings, position);
        if (character!=0) {
            result.append(character);
        } else {
            result.append(substitute);
        }
    }

    return result.toString();
}

/**
 * Compares the character at the specified position in all input strings
 * @param strings
 * @param position
 * @return character found is same in all strings, otherwise 0;
 */
private char allCharactersMatch(String[] strings, int position) {
    char found = 0;
    for (String string : strings) {
        if (string.length()<=position) return 0;
        if (string.charAt(position)!=found && found!=0) return 0;
        found = string.charAt(position);
    }
    return found;
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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