Java 从另一个字符串中出现的字符串打印字符

Java 从另一个字符串中出现的字符串打印字符,java,string,Java,String,我希望打印另一个字符串中出现的字符串中的字符;不出现的字符替换为星号 例如: 字符串1:helloworld 字符串2:hord 输出应为: h***o**r*d 仅打印字符串1中出现的字符串2中的字符;字符串1中未出现在字符串2中的字符表示为星号 我编写了以下代码: public class ProgramonStrings { public static void main(String[] args) { { String str1 = "hellowor

我希望打印另一个字符串中出现的字符串中的字符;不出现的字符替换为星号

例如:

字符串1:helloworld 字符串2:hord 输出应为:

h***o**r*d 仅打印字符串1中出现的字符串2中的字符;字符串1中未出现在字符串2中的字符表示为星号

我编写了以下代码:

public class ProgramonStrings {

    public static void main(String[] args) {
    {
        String str1 = "helloworld";
        String str2 = "hord";
        StringBuffer sbf = new StringBuffer();
        StringBuffer sbf2 = new StringBuffer();
        String output;
        for (int i = 0; i < str1.length(); i++) {
            int count = 0;
            for (int j = 0; j < str2.length(); j++) {
                if (str1.charAt(i) == str2.charAt(j)) {
                    sbf.append(str1.charAt(i));
                    count++;
                }
            }
            if (count == 0) {
                sbf.append('*');
            }
        }
        output = sbf.toString();
        for (int i = 0; i < output.length(); i++) {
            int count = 0;
            if (output.charAt(i) != '*') {
                for (int j = i + 1; j < output.length(); j++) {
                    if (output.charAt(j) == output.charAt(i)) {
                        //output.replace(output.charAt(j), '*');
                        sbf2.append('*');
                        count++;
                    }
                }
            }
            if (count == 0) {
                sbf2.append(output.charAt(i));
            }
        }
        System.out.println(sbf2);
        System.out.println(sbf2);
    }    
}
从那时起,我得到的输出是:h****或*d


因此,任何人都可以修改我的程序以获得适当的输出,即:h***o**r*d

您只需要在文本字符串上迭代一次。对其进行迭代,并将字符串中的当前字符与第二个字符串中的下一个字符进行比较。如果相等,则使用第二个字符串中的字符,即递增指针。否则打印星号字符*

如果指针
public static void main(String[] args) {

    String str1 = "helloworld";
    String str2 = "hord";
    int pointer = 0;

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < str1.length(); i++) {
        if (pointer < str2.length() && str1.charAt(i) == str2.charAt(pointer)) {
            sb.append(str1.charAt(i));
            pointer++;
        }
        else {
            sb.append('*');
        }
    }
    System.out.println(sb.toString());
}

请注意,最好使用StringBuilder而不是StringBuffer。

您可以使用与此类似的代码来提供所需的输出

public class ProgramOnStrings {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Compare cobj=new Compare();
    cobj.compareStrings();
}

}

class Compare
{
String s1="helloworld";
String s2="hord";
int array[];
String small,big;

Compare()
{
    if(s1.length()<s2.length())
      {

          small=s1;
          big=s2;
          array=new int[small.length()];

      }
      else
      {
        small=s2;
        big=s1;
        array=new int[s2.length()];  

      }
    System.out.println("small\t"+small+"\tbig\t"+big);
}

public void compareStrings()
{
    int j=0;
   for(int i=0;i<big.length();i++)
  {

      if(small.charAt(j)==big.charAt(i))
      {
         array[j]=i;
         System.out.println("if: The selected positions are"+i);
        j++;  
      }


  }

  int m=0;
  for(int k=0;k<big.length();k++)
  {
      if(array[m]==k)
      {
          System.out.print(small.charAt(m));
          m++;
      }
      else
      {
      System.out.print("*");
      }
 }
}

}

至少修复您的缩进输出包含您想要的字符串。为什么是第二个循环?您没有提供有关问题的足够信息。您希望如何使用这两个字符串生成第三个字符串?@Eypros我已经提到,这两个输入字符串非常感谢@Veeraminice,希望听到您朋友的回复。如果你以后有任何问题,请在这个网站上发帖,非常感谢@mcemperon