public static void sop1(String source, String text) {
IntStream sourceStream = source.codePoints();
IntStream textStream = text.codePoints();
List<Integer> textCollect = textStream.boxed().collect(Collectors.toList());
int textSize = textCollect.size();
int asInt = textCollect.get(0);
System.out.println(asInt);
List<Integer> collect = sourceStream.boxed().collect(Collectors.toList());
int size = collect.size();
int index = collect.indexOf(asInt);
int start = 0;
int end = 0;
if (index - 5 <= 0) {
start = 0;
} else {
start = index - 5;
}
if (index + 5 + textSize >= size) {
end = size;
} else {
end = index + 5 + textSize;
}
System.out.println(size);
System.out.println(index);
System.out.println(start);
System.out.println(end);
List<Integer> subList = collect.subList(start, end);
Stream<Integer> integerStream = subList.parallelStream();
String str = integerStream.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString();
System.out.println("result = " + str);
}
soure:原String
text:需要定位的位置
demo已前后5个单位做截取。