需求就是:如何判断两个字符串 或多个字符串中 相同的字符
比如:String a = "aaabbcc"; String b = "addff" ;String c = "eeffa" ; 
如何能判断得出a,b,c字符串中有共同的字符 a 啊

解决方案 »

  1.   

    循环遍历26个字符,用indexOf判断它们哪一个存在嘛
      

  2.   

    如果一个String a= new String("-1,2,4a");
    是这样的话 也能用26个字符吗
      

  3.   

    有一个比较笨的方法。先比较s1和s2中是否有重复的字符,如果有的话就把他们放到另一个数组中(自己声明的)。然后在遍历s3,找出相同的。。注意用递归方法!!
      

  4.   

    用正则表达式,匹配想要的字符串。与三个变量match一下。如果都为true就是都包含了。很快的
      

  5.   

    String a = "aaabbcc";
    String b = "addff";
    String c = "eeffa";
    String[] ss = { a, b, c };
    List<String> tmp = new ArrayList<String>();// 用于记录字符串中出现的所有字符
    for (String s : ss) {
        String[] chars = s.split("");
        for (String ch : chars) {
    if (!tmp.contains(ch)) {
        tmp.add(ch);
    }
        }
    }
    List<String> result = new ArrayList<String>();// 用于记录结果
    for (String ch : tmp) {
        boolean flag = true;
        for (String s : ss) {
    if (s.indexOf(ch) == -1) {
        flag = false;
    }
        }
        if (flag) {
    result.add(ch);
        }
    }
    for (String ch : result) {
        System.out.print(ch + " ");
    }
        }
      

  6.   


    并不是匹配字符串,而是字符串中的字符
    不是要的true 和 false 的结果 
    是它们几个字符串中的共同的字符,这个用正则表达式 真不知道怎么写
      

  7.   

    用indexOf也可以啊
    String.indexOf("a")
      

  8.   


    public class A {
    public String[] getSameAppearWord(String[] strs) {
    List<String> list = new ArrayList<String>();
    if(strs.length>0) {
    Set<String> haveWord = this.getHaveWord(strs[0]);
    for(String word : haveWord) {
    boolean allHave = true;
    for(String oneStr : strs) {
    if(oneStr.indexOf(word) == -1) {
    allHave = false;
    break;
    }
    }
    if(allHave) list.add(word);
    }
    }
    return list.toArray(new String[]{});
    }
    //看第一个字符串中不重复的字符有哪些
    private Set<String> getHaveWord(String word) {
    Set<String> set = new HashSet<String>();
    if(word.length()>0) {
    for(int i=0;i<word.length()-1;i++) {
    set.add(word.substring(i,i+1));
    }
    }
    return set;
    }
    public static void main(String[] args) {
    String a = "aaabbcc"; 
    String b = "addffc" ;
    String c = "eeffac" ; 
    String[] strs = {a,b,c};
    A test = new A();
    String[] sameAppearWord = test.getSameAppearWord(strs);
    for(String str : sameAppearWord) System.out.println(str);
    }}
      

  9.   

    public class TestSameChar { /**
     * 判断多个字符串中重复的字符,并打印输出
     * @param args
     */
    public static void main(String[] args) {
    String a = "aaabbccfe"; 
    String b = "addffe";
    String c = "eeffa";
    String totalString[] = {a, b, c}; //先将多个字符串归并到一个字符串数组中,方便取用
    //字符串数组用来判断下一个取的字符是否已经在数组中存在,如果是则代表取的是重复的字符,直接跳过
    char[] arr = new char[a.length()];
    System.out.println("重复的字符为:");
    for(int i=0; i<a.length(); i++) {
    boolean f = true; //f代表是否找到重复的字符
    char ch = a.charAt(i);
    if(!isContains(arr, ch)) { //判断是否取到了重复的字符
    arr[i] = ch; //如果没有,则先将其存储到字符数组中
    for(int j=1; j<totalString.length; j++) { //循环遍历每一个字符串数组
    if(totalString[j].indexOf(ch) == -1) {
    f = false;
    break;
    }
    }
    if(f)
    System.out.print(ch + " ");
    }
    }
    }

    private static boolean isContains(char[] arr, char c) {
    for(int i=0; i<arr.length; i++) {
    if(arr[i] == c)
    return true;
    }
    return false;
    }
    }
      

  10.   

    我再给你一个算法:使用三重循环package j2se.string;/**
     * 找出a b c字符串中含有相同的字母
     * @author feng
     *
     */public class CsdnOne {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    TestString ts = new TestString();
    ts.testABC();
    }}class  TestString{


    String a = "aaabbcc";
    String b = "addff" ;
    String c = "eeffa" ; 
    public void testABC(){
    for(int i=0;i<a.length();i++){
    for(int j=0;j<b.length();j++){
    for(int k=0;k<c.length();k++){
    if(a.charAt(i)==b.charAt(j)){
    if(a.charAt(i)==c.charAt(k)){
    System.out.println("a b c中都含有的字母有:"+a.charAt(i));
    }
    }
    }
    }
    }
    }
    }
      

  11.   

    package t1;import java.util.regex.Matcher;
    import java.util.regex.Pattern;import java.util.ArrayList;
    import java.util.List;/**
     * 此题的正则表达式
     * 
     * @author forfelicity
     * 
     */
    public class Search {
    public void search(String a, String b, String c) {
    int length = a.length();
    String regex = null;
    List<String> list = new ArrayList<String>();
    for (int i = 0; i < length; i++) {
    regex = a.charAt(i) + "";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher1 = pattern.matcher(b);
    Matcher matcher2 = pattern.matcher(c);
    if (matcher1.find() && matcher2.find()) {
    list.add(regex);
    }
    }
    if (!list.isEmpty()) {
    System.out.println("都含有的字母有:");
    for (String s : list)
    System.out.print(s);
    } else {
    System.out.println("Not Found 都含有的字母");
    }
    } public static void main(String args[]) {
    Search test = new Search();
    String a = new String("abc");
    String b = new String("bcd");
    String c = new String("cde");
    test.search(a, b, c);
    }}