java - 从第二个字符串中的第一个字符串中查找子字符串?
问题描述
更新:我有两个字符串。
String a = "is am are has have was " //Helping verbs
String b= " Robert and william are English Robert has a car he was living in England " //Sentence
更新:从字符串 a 中选择第一个子字符串,然后在字符串 b 的子字符串中搜索该子字符串。如果找到,则返回两者的索引号。然后继续在第二个字符串中找到第一个字符串的第二个子字符串,依此类推直到最后一个。
解决方案
请在下面找到代码:-
String stringA = "one two three four five six " ;
String stringB = " eleven two four twelve thirteen ";
String[] stringOne = stringA.split(" ");
String[] stringTwo = stringB.trim().split(" ");
Map<Integer,Integer> mapMatchingStrings = new HashMap<>();
IntStream.range(0,stringOne.length).forEach(indexOfStringOne -> IntStream.range(0,stringTwo.length).forEach(indexOfStringTwo ->
{
if(Objects.equals(stringOne[indexOfStringOne],stringTwo[indexOfStringTwo])){
mapMatchingStrings.put(indexOfStringOne,indexOfStringTwo);
}
}
));
System.out.println(mapMatchingStrings);
输出 :-
{1=1, 3=2}
推荐阅读
- android - getView() 方法中的 Parent 到底是什么
- delphi - Delphi 源代码仅具有 LF 与 CRLF 导致 IDE 中的源损坏
- javascript - 当代码在项目根目录之外时,babel-loader reactjs Unexpected Token
- unit-testing - 如何比较/匹配模拟中的闭包?
- mysql - 优先级查询如何工作(如果满足条件,一个值优先于另一个值)
- batch-file - 通过命令行使用 Inkscape 中的“干净文档”
- python - 如何从文本文件中消除重复的 IP
- jquery - jQuery toggleclass 按钮动作到复选框动作
- javascript - 我怎样才能让 clearInterval 停止我的计时器?
- python-3.x - 如何使用 for 循环同时打印两个变量?