java 获取字符串安全截取长度


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SafeSub {

	public static void main(String[] args) {
		
		String html="test来自<em>东方</em><font color='red'><font>你好</font></font>java";
		List<String> not_sub_keywords=new ArrayList<>();
		
		not_sub_keywords.add("<font>");
		not_sub_keywords.add("<font color='red'>");
		not_sub_keywords.add("</font>");		
		not_sub_keywords.add("<em>");
		not_sub_keywords.add("</em>");
		
		int safeLen=getSafeLen(html,16,not_sub_keywords);//getSafeLen(html,35,datas);
		System.out.println(safeLen);
		System.out.println(html.substring(0,safeLen));

	}

	//获取搜索结果摘要的安全截取长度,不会截取到not_sub_keywords标签内。
	public static int getSafeLen(String html,int len, List<String> not_sub_keywords) {
		int safeLen=len;
		if(len>html.length()-1)
		{
			len=html.length()-1;				
		}
		List<Integer> safeLengths=new ArrayList<Integer>();
		for (String data : not_sub_keywords) {
			int startIndex=len-data.length()-1;
			int endIndex=len+data.length()+1;
			if(startIndex<0)
			{
				startIndex=0;
			}
			if(endIndex>html.length()-1)
			{
				endIndex=html.length()-1;				
			}
			
			String sub=html.substring(startIndex,endIndex);
			int dataLen=sub.lastIndexOf(data);
			if(dataLen!=-1)
			{
				int safeStartLen=startIndex+dataLen+data.length();
				safeLengths.add(safeStartLen);
			}			
		}
		//System.out.println(safeLengths.size());
		if(safeLengths.size()>0)
		{
			Collections.sort(safeLengths);
			safeLen=safeLengths.get(safeLengths.size()-1);
			
		}
		return safeLen;
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java字符串操作大全,适合初学者,浅显易懂 部JAVA字符串操作 2008-07-11 15:39:42| 分类: JAVA | 标签: |字号大中小 订阅 . JAVA字符串的方法 String a = "53c015"; //Integer.parseInt(s, radix) radix设置为10,表示10进制,16表示16进制啦 int i = Integer.parseInt(a, 16); 1、length() 字符串长度   例:char chars[]={'a','b'.'c'};     String s=new String(chars);     int len=s.length(); 2、charAt() 截取一个字符   例:char ch;     ch="abc".charAt(1); 返回'b' 3、getChars() 截取多个字符   void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)   sourceStart指定了子串开始字符的下标,sourceEnd指定了子串结束后的下一个字符的下标。因此,子串包含从sourceStart到sourceEnd-1的字符。接收字符的数组由target指定,target中开始复制子串的下标值是targetStart。   例:String s="this is a demo of the getChars method.";     char buf[]=new char[20];     s.getChars(10,14,buf,0); 4、getBytes()   替代getChars()的一种方法是将字符存储在字节数组中,该方法即getBytes()。 5、toCharArray() 6、equals()和equalsIgnoreCase() 比较两个字符串 7、regionMatches() 用于比较一个字符串中特定区域与另一特定区域,它有一个重载的形式允许在比较中忽略大小写。   boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)   boolean regionMatches(boolean ignoreCase,int startIndex,String str2,int str2StartIndex,int numChars) 8、startsWith()和endsWith()   startsWith()方法决定是否以特定字符串开始,endWith()方法决定是否以特定字符串结束 9、equals()和==   equals()方法比较字符串对象中的字符,==运算符比较两个对象是否引用同一实例。   例:String s1="Hello";     String s2=new String(s1);     s1.eauals(s2); //true     s1==s2;//false 10、compareTo()和compareToIgnoreCase() 比较字符串 11、indexOf()和lastIndexOf()   indexOf() 查找字符或者子串第一次出现的地方。   lastIndexOf() 查找字符或者子串是后一次出现的地方。 12、substring()   它有两种形式,第一种是:String substring(int startIndex)          第二种是:String substring(int startIndex,int endIndex) 13、concat() 连接两个字符串 14 、replace() 替换   它有两种形式,第一种形式用一个字符在调用字符串中所有出现某个字符的地方进行替换,形式如下:   String replace(char original,char replacement)   例如:String s="Hello".replace('l','w');   第二种形式是用一个字符序列替换另一个字符序列,形式如下:   String replace(CharSequence original,CharSequence replacement) 15、trim() 去掉起始和结尾的空格 16、valueOf() 转换为字符串 17、toLowerCase() 转换为小写 18、toUpperCase() 转换为大写 19、StringBuffe

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值