StringUtil

package util;

import java.util.ArrayList;
import java.util.List;
/**
*
*
*/
public class StringUtil {


/***
*
* @param s 要匹配的字符窜 如:aaa${user}bbb${people.name}cccc${people.age}ddd${people.kk.ah.fgh}
* @param pref 字符窜前最 如:${
* @param end  字符窜后最 如:}
* @return  如:List${}间的字符窜,如:user对应:${user},people.name对应:{people.name}
*/
public static List findAllAssignKeyMiddleChar(String s,String pref,String end){
if(BlankUtil.isBlank(pref) || BlankUtil.isBlank(end) || BlankUtil.isBlank(s)){
return null;
}
List result=new ArrayList();
//find out all pref in s positions
List allPositions=StringUtil.findAllPosition(s, pref);
String temp=null;
for(int i=0,len=allPositions.size();i<len;i++){
Integer position=(Integer) allPositions.get(i);
int tempBefore=position.intValue();
int tempAfter=StringUtil.findAfterPosition(s, end, tempBefore);
temp=s.substring(tempBefore+pref.length(), tempAfter);
result.add(temp);
}
return result;
}

public static List findAllPosition(String s, String f) {
List positions = new ArrayList();
// int lastPosition=0;
if (null == s || "".equals(s)) {
return positions;
}
int length = f.length();
for (int i = 0; i < s.length() - length + 1; i++) {
String sub = s.substring(i, i + length);
if (f.equalsIgnoreCase(sub)) {
positions.add(Integer.valueOf(String.valueOf(i)));
}

}
return positions;
}


public static int findLastPosition(String s, String specialKey, int beforePosition) {
int lastPosition = -1;
if (null == s || "".equals(s)) {
return 0;
}
int length = specialKey.length();
for (int i = 0; i < beforePosition - length + 1; i++) {
String sub = s.substring(i, i + length);
if (specialKey.equalsIgnoreCase(sub)) {
lastPosition = i;
}
}

return lastPosition;
}

public static int findAfterPosition(String s, String specialKey, int beforePosition) {
int lastPosition = -1;
if (null == s || "".equals(s)) {
return 0;
}
int length = specialKey.length();
for (int i = beforePosition; i < s.length() - length + 1; i++) {
String sub = s.substring(i, i + length);
if (specialKey.equalsIgnoreCase(sub)) {
lastPosition = i;
break;
}
}

return lastPosition;
}



/***
*
* @param s
*            要比较的string
* @param f
*            要过滤得string
* @return 如果s包含有f return true,没有return false
*/
public static boolean ifContentString(String s, String f) {
if (null == s || "".equals(s)) {
return false;
}
int length = f.length();
for (int i = 0; i < s.length() - length + 1; i++) {
String sub = s.substring(i, i + length);
if (f.equalsIgnoreCase(sub)) {
return true;
}

}
return false;
}

/**
* @param args
*/
public static void main(String[] args) {
System.out.println(ifContentString("who am I", "am"));
}

}

你可能感兴趣的:(java,String)