加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式来匹配文本串中的空白符

发布时间:2020-12-14 01:30:59 所属栏目:百科 来源:网络整理
导读:/** * * pTitle: test/p * pDescription: 用正则表达式来匹配文本串中的空白符,但是在正则表达式中加入了行的开头和行的结尾匹配符之后,匹配效果就不行了/p * */public void test() {// String regex = "s+";// 输出结果:我的测试文件.xmlString regex
	/**
	 * 
	 * <p>Title: test</p> 
	 * <p>Description: 用正则表达式来匹配文本串中的空白符,但是在正则表达式中加入了行的开头和行的结尾匹配符之后,匹配效果就不行了</p>  
	 *
	 */
	public void test() {
		// String regex = "s+";// 输出结果:我的测试文件.xml
		String regex = "^s+$";// 输出结果:我的测试 文 件 .xml
		String str = "我的测试 文	件 .xml";
		String replaceAll = str.replaceAll(regex,"");
		System.out.println(replaceAll);

	}
	
	/**
	 * 
	 * <p>
	 * Title: removeSpacesFromFileName
	 * </p>
	 * <p>
	 * Description: 将文件名中的空格删除
	 * </p>
	 * 
	 * @param filePath
	 * @return
	 *
	 */
	public static boolean removeSpacesFromFileName(String filePath) {
		File file = new File(filePath);
		String path = file.getPath();
		path = path.replaceAll("s+","").replaceAll(" ","");
		return file.renameTo(new File(path));
	}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读