日期:2014-05-20  浏览次数:20710 次

查找字符串中的字符
在字符串:/lks/koa/lks_news.nsf/VD_Portlet_View?ReadViewEntries&PreFormat&Count=2&RestrictToCategory=%u516C%u53F
中,怎么找到Count=2这个字符呢?


------解决方案--------------------
单独的String的话!

public class test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "===Count=2== ";

int ind = str.indexOf( "Count=2 ");
if (ind != -1) {
str = str.substring(ind, ind + 7);
}
System.out.println(str);
}

}

(2)如果是url的话!

我们可以用request.getparameter(Count)
------解决方案--------------------
用str.replaceFirst( "Count=2 ", "Count=3 ");
或者其他的方法看需求了!


public class test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "===Count=2== ";

String strRet = " ";

int ind = str.indexOf( "Count=2 ");
if (ind != -1) {
strRet = str.substring(ind, ind + 7);

str = str.replaceFirst( "Count=2 ", "Count=3 ");
}
System.out.println(str);
}

}


------解决方案--------------------
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test2 {
  public static void main(String[] args) {
    String strcount = "/lks/koa/lks_news.nsf/VD_Portlet_View? " +
         "ReadViewEntries&PreFormat&Count=2& " +
         "RestrictToCategory=%u516C%u53F ";
    Pattern pattern = Pattern.compile( "(&Count\\=)\\d+(&) ");
    Matcher matcher = pattern.matcher(strcount);
    matcher.find();
    int modify = 3;
    strcount = strcount.substring(0, matcher.end(1)) + modify +
        strcount.substring(matcher.start(2));
    System.out.println(strcount);
  }
}