我现在有这么个内容cAbcdAbcdeAbcdefAbcde我需要查找关键字A,并输出A前后2个字符的内容,如果开始检索时A前面只有一个字符那就显示一个字符,如果超过2个字符则在前面加“...”,下一个A与前一个A之间不足或等于4个字符时都显示,超过4个字符时在两个字符之后加“...”例如上面这个内容显示为:
cAbcdAbcdeAbc...efAbc...
我是这么写的:
string key="A";
int begin;
int eng;
int index=content.indexof(key);记录下标
while(index!=-1){开始循环
if(index<0)continue;
if(index>2){
begin=index-2;大于2个字符时起始位置
}else{
begin=0;小于等于两个字符时起始位置
}
end=begin+key.length()+4;结束位置
if(end>content.lenth()){
end=content.length();
}
int temp=content.subString(begin,end);截取这段字符
int text=sb.append(temp);存入stringBuffer中
}
下面不会写了···求大神指导下
cAbcdAbcdeAbc...efAbc...
我是这么写的:
string key="A";
int begin;
int eng;
int index=content.indexof(key);记录下标
while(index!=-1){开始循环
if(index<0)continue;
if(index>2){
begin=index-2;大于2个字符时起始位置
}else{
begin=0;小于等于两个字符时起始位置
}
end=begin+key.length()+4;结束位置
if(end>content.lenth()){
end=content.length();
}
int temp=content.subString(begin,end);截取这段字符
int text=sb.append(temp);存入stringBuffer中
}
下面不会写了···求大神指导下
public static void main(String[] args) {
// TODO Auto-generated method stub String content = "cAbcdAbcdeAbcdefAbcde";
String key = "A"; int begin;
int eng;
int index = content.indexOf(key);
StringBuffer sb = new StringBuffer();
while (index != -1) {
String temp = null;
if (content.length()>= index+ key.length()+2 ){
temp = content.substring(0, index+ key.length()+2);
content = content.substring(index+ key.length()+2);
int idx = content.indexOf(key);
if (idx > 2 || idx == -1){
temp = temp + "..";
}
}else{
temp = content;
content = "";
}
if (index>2){
temp = temp.substring(index - 2);
}
index = content.indexOf(key);
sb.append(temp);
}
System.out.println(sb);
}
改了一点点
public static void main(String[] args) {
// TODO Auto-generated method stub String content = "ccccAbcdAbcdeAbcdefAbc";
String key = "A"; int begin;
int eng;
int index = content.indexOf(key);
StringBuffer sb = new StringBuffer();
while (index != -1) {
String temp = null;
if (content.length()>= index+ key.length()+2 ){
temp = content.substring(0, index+ key.length()+2);
content = content.substring(index+ key.length()+2);
int idx = content.indexOf(key);
if (idx == -1 && content.length()>0){
temp = temp + "..";
}
}else{
temp = content;
content = "";
}
if (index>2){
temp = ".." + temp.substring(index - 2);
}
index = content.indexOf(key);
sb.append(temp);
}
System.out.println(sb);
}
System.out.println(a.replaceAll("(?<=^|A[^A]{2})([^A]+?)(?=[^A]{2}A|$)", "..."));
你想要输出什么样子的呢?
public static void main(String[] args) {
System.out.println(replace("AAAAABBBBBCCCCCCDDDDDDEEEEEE", "A"));
}
public static String replace(String text, String key) {
String regex = "(?<=^|KEY[^KEY]{2})([^KEY]+?)(?=[^KEY]{2}KEY|$)".replace("KEY", key);
return text.replaceAll(regex, "...");
}
这样不行吗,把关键字作为参数,还有其他地方可以改,包括要替换成...还是别的,保留附近几位(正则表达式中的2),都可以提炼成参数。
{
public void test(String content, String key)
{
StringBuffer sb = new StringBuffer();
// 查询是否存在key
int index1 = content.indexOf(key);
if (index1 > 0)
{
// 截取前后字符串
sb.append(content.substring((index1 - 3) < 0 ? 0 : (index1 - 3),
(index1 + 3) > content.length() ? content.length()
: (index1 + 3))); int index2 = 0;
for (int i = index1; i < content.length();)
{
String temp1 = content.substring(i + 1, content.length());
// 存放临时变量 index2 = temp1.indexOf(key);
if (index2 > 0)
{
if (index2 <= 4 && index2 >= 2)
{
sb.append(temp1.substring(2, (index2 + 3) > temp1
.length() ? temp1.length() : (index2 + 3)));
}
else if (index2 > 4)
{
sb.append("..."
+ temp1.substring(index2 - 2,
(index2 + 3) > temp1.length() ? temp1
.length() : (index2 + 3)));
}
else if (index2 < 2)
{
sb.append(temp1.substring(index2, (index2 + 3) > temp1
.length() ? temp1.length() : (index2 + 3)));
}
i = i + index2 + 1;
// temp1=temp1.substring(index2 + 1, temp1.length());
}
else
{
sb.append("...");
break;
} }
}
System.out.println(sb.toString());
} public static void main(String[] args)
{
String content = "cAbcdAbcdddeAbcdefAbcde";
String key = "A";
AAA aaa = new AAA();
aaa.test(content, key);
}
}