从一段字符串中截取包含给定字符串的子字符串

橄榄丝 2010-05-21 01:38:17
如题,子字符串不要包含 逗号,分号,句号,省略号,换行 等
...全文
491 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hepeng_8 2010-05-22
  • 打赏
  • 举报
回复
查java API 中String的方法
jlhjlh521 2010-05-22
  • 打赏
  • 举报
回复
是学习还是工作啊 要是学习应该自己查api看看,百度上搜下,不要来这里直接找答案。要是工作的话为完成任务有情可原。
liyu1006 2010-05-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zlx5219 的回复:]
String replaceAll(String regex, String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
这个方法应该可以搞定吧
[/Quote]

学习中
zihuzihuzihu 2010-05-21
  • 打赏
  • 举报
回复
xiayuqijava 2010-05-21
  • 打赏
  • 举报
回复
查java API 中String的方法
zlx5219 2010-05-21
  • 打赏
  • 举报
回复
String replaceAll(String regex, String replacement)
使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
这个方法应该可以搞定吧
Silence_Smile 2010-05-21
  • 打赏
  • 举报
回复
用正则表达式,我给你个例子
public String HtmlToTextGb2312(String inputString) 
{
String htmlStr = inputString; //含html标签的字符串
String textStr ="";
Pattern p_script;
Matcher m_script;
Pattern p_style;
Matcher m_style;
Pattern p_html;
Matcher m_html;
Pattern p_houhtml;
Matcher m_houhtml;
Pattern p_spe;
Matcher m_spe;
Pattern p_blank;
Matcher m_blank;
Pattern p_table;
Matcher m_table;
Pattern p_enter;
Matcher m_enter;

try {
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
//定义script的正则表达式.
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
//定义style的正则表达式.
String regEx_html = "<[^>]+>";
//定义HTML标签的正则表达式
String regEx_houhtml = "/[^>]+>";
//定义HTML标签的正则表达式
String regEx_spe="\\&[^;]+;";
//定义特殊符号的正则表达式
String regEx_blank=" +";
//定义多个空格的正则表达式
String regEx_table="\t+";
//定义多个制表符的正则表达式
String regEx_enter="\n+";
//定义多个回车的正则表达式

p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
m_script = p_script.matcher(htmlStr);
htmlStr = m_script.replaceAll(""); //过滤script标签

p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
m_style = p_style.matcher(htmlStr);
htmlStr = m_style.replaceAll(""); //过滤style标签

p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
m_html = p_html.matcher(htmlStr);
htmlStr = m_html.replaceAll(""); //过滤html标签

p_houhtml = Pattern.compile(regEx_houhtml,Pattern.CASE_INSENSITIVE);
m_houhtml = p_houhtml.matcher(htmlStr);
htmlStr = m_houhtml.replaceAll(""); //过滤html标签

p_spe = Pattern.compile(regEx_spe,Pattern.CASE_INSENSITIVE);
m_spe = p_spe.matcher(htmlStr);
htmlStr = m_spe.replaceAll(""); //过滤特殊符号

p_blank = Pattern.compile(regEx_blank,Pattern.CASE_INSENSITIVE);
m_blank = p_blank.matcher(htmlStr);
htmlStr = m_blank.replaceAll(" "); //过滤过多的空格

p_table = Pattern.compile(regEx_table,Pattern.CASE_INSENSITIVE);
m_table = p_table.matcher(htmlStr);
htmlStr = m_table.replaceAll(" "); //过滤过多的制表符


p_enter = Pattern.compile(regEx_enter,Pattern.CASE_INSENSITIVE);
m_enter = p_enter.matcher(htmlStr);
htmlStr = m_enter.replaceAll(" "); //过滤过多的制表符

textStr = htmlStr;

}catch(Exception e)
{
System.err.println("Html2Text: " + e.getMessage());
}

return textStr;//返回文本字符串
}
Fairy_dy 2010-05-21
  • 打赏
  • 举报
回复
1 用String的subString()就可以
2 至于特殊的符合,加个条件判断一下,你把它不包含就可以

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧