Java将两个字符串中重复的元素取出来赋给另一个字符串

版权声明:版权所有@万星明 https://blog.csdn.net/qq_19533277/article/details/83039688
import java.util.Scanner;

/**
 * 现在有两个用户输入的字符串,将这两个字符串中重复的元素取出来赋给另一个字符串
 * @author 万星明
 * @version 1.0
 * @time
 */
public class Work7 {
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		
		System.out.println("请输入字符串1:");
		String st1 = sc.next();
		System.out.println("请输入字符串2:");
		String st2 = sc.next();
		System.out.println("重复字符串为:"+repeat(st1,st2));
	}
	public static String repeat(String st1,String st2) {
		String s3 = "";
		for(int i=0;i<st1.length();i++) {
			for(int j=0;j<st2.length();j++) {
				if(st1.charAt(i)==st2.charAt(j)) {
					s3 = s3+st2.charAt(j);
				}
			}
		}
		return s3;
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_19533277/article/details/83039688
今日推荐