如何在Java中修剪不间断空间? [英] How to trim no-break space in Java?

查看:89
本文介绍了如何在Java中修剪不间断空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入了一个输入文件,我需要处理并丢弃所有空格,包括不间断的空格 U + 00A0 又名&#160; (您可以在记事本中按 Alt 然后输入 0 1 < kbd> 6 0 来自键盘的数字键盘。)或任何其他形式的空白区域。我试过 String.trim() 但它没有修剪 U + 00A0



我是否需要明确检查 U + 00A0 然后 trim()或者是否容易在Java中修剪各种空格的方法?

解决方案

虽然&#160; 是一个非破坏性空间(一个不希望被视为空格的空间) ,你可以修剪一个字符串,同时用一个简单的正则表达式保留字符串中的每个&#160;

  string.replaceAll((^ \\h *)|(\\\\ * *)$,)




  • \ h 是一个水平空格字符: [\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ul>

    如果您使用的是JDK8之前版本,则需要明确使用字符列表而不是 \h


    I've input an input file which I need to process and discard all the white-spaces, including non-breaking space U+00A0 aka &#160; (You can produce it in Notepad by pressing Alt and then typing 0 1 6 0 from the keyboard's numeric pad.) or any other form of white space. I have tried String.trim() but it doesn't trim U+00A0.

    Do I need to explicitly check for U+00A0 and then trim() or is there an easy way to trim all kinds of white-spaces in Java?

    解决方案

    While &#160; is a non breaking space (a space that does not want to be treated as whitespace), you can trim a string while preserving every &#160; within the string with a simple regex:

    string.replaceAll("(^\\h*)|(\\h*$)","")
    

    • \h is a horizontal whitespace character: [ \t\xA0\u1680\u180e\u2000-\u200a\u202f\u205f\u3000]

    If you are using a pre JDK8 Version, you need to explicitly use the list of chars instead of \h.

    这篇关于如何在Java中修剪不间断空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆