如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public static void main(string[] args) { string str1 = "刘烨,孙坚,王二小,蜘蛛侠,钢铁侠,毛剑卿" ; string str2 = "王二小,李占军,刘胡兰,毛剑卿" ; string[] arr1 = str1.split( "," ) ; string[] arr2 = str2.split( "," ) ; stringbuffer sb = new stringbuffer(); for ( int i = 0 ; i < arr2.length; i++){ for ( int j = 0 ; j < arr1.length; j++){ if (arr1[j].equals(arr2[i])){ sb.append(arr1[j] + "," ) ; } } } system.out.println( "结果:" + sb.tostring().substring( 0 , sb.tostring().length() - 1 )); } |
输出: "结果:王二小,毛剑卿"
以上这篇java提取两个字符串中的相同元素方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/wl724120268/article/details/53506320