<bdo id='f7xNa'></bdo><ul id='f7xNa'></ul>

      1. <tfoot id='f7xNa'></tfoot>
      2. <small id='f7xNa'></small><noframes id='f7xNa'>

        <legend id='f7xNa'><style id='f7xNa'><dir id='f7xNa'><q id='f7xNa'></q></dir></style></legend>
        <i id='f7xNa'><tr id='f7xNa'><dt id='f7xNa'><q id='f7xNa'><span id='f7xNa'><b id='f7xNa'><form id='f7xNa'><ins id='f7xNa'></ins><ul id='f7xNa'></ul><sub id='f7xNa'></sub></form><legend id='f7xNa'></legend><bdo id='f7xNa'><pre id='f7xNa'><center id='f7xNa'></center></pre></bdo></b><th id='f7xNa'></th></span></q></dt></tr></i><div id='f7xNa'><tfoot id='f7xNa'></tfoot><dl id='f7xNa'><fieldset id='f7xNa'></fieldset></dl></div>

        Java 在字符串中看不到空格

        时间:2023-07-28
            <bdo id='YcEua'></bdo><ul id='YcEua'></ul>
          • <tfoot id='YcEua'></tfoot>
            <i id='YcEua'><tr id='YcEua'><dt id='YcEua'><q id='YcEua'><span id='YcEua'><b id='YcEua'><form id='YcEua'><ins id='YcEua'></ins><ul id='YcEua'></ul><sub id='YcEua'></sub></form><legend id='YcEua'></legend><bdo id='YcEua'><pre id='YcEua'><center id='YcEua'></center></pre></bdo></b><th id='YcEua'></th></span></q></dt></tr></i><div id='YcEua'><tfoot id='YcEua'></tfoot><dl id='YcEua'><fieldset id='YcEua'></fieldset></dl></div>
              <tbody id='YcEua'></tbody>
            <legend id='YcEua'><style id='YcEua'><dir id='YcEua'><q id='YcEua'></q></dir></style></legend>

              • <small id='YcEua'></small><noframes id='YcEua'>

                  本文介绍了Java 在字符串中看不到空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  所以,我正在尝试解析一些包含多行文本的文本文件.我的工作是浏览所有单词并将它们打印在文件中.

                  So, I'm trying to parse some text file which has multiple lines of text. My job is to go through all words and print them out in file.

                  所以,我阅读了所有的行,我正在循环它们并用空格分隔每一行,如下所示:

                  So, I read all lines, I'm looping through them and splitting every line by spaces, like this:

                  line.split("\s+");

                  现在,问题是在某些情况下 Java 看不到两个单词之间的空格...

                  Now, the problem is that in some cases Java does not see space between two words...

                  我也试图遍历有空格但 Java 看不到它的字符串,并且 Character.isSpaceChar(char) 返回 true...

                  I was also trying to loop through string which has space but Java doesn't see it, and Character.isSpaceChar(char) returned true...

                  现在我完全糊涂了……

                  代码如下:

                  public void createMap(String inputPath, String outputPath)
                              throws IOException {
                                  File f = new File(inputPath);
                          FileWriter fw = new FileWriter(outputPath);
                          List<String> lines = Files.readAllLines(f.toPath(),
                                  StandardCharsets.UTF_8);
                          for (String l : lines) {
                              for (String w : l.split("\s+")) {
                                  if (isNotRubbish(w.trim())) {
                                      fw.write(w.trim() + "
                  ");
                                  }
                              }
                          }
                          fw.close();
                      }
                  private boolean isNotRubbish(String w) {
                          Pattern p = Pattern.compile("@?\p{L}+",
                                  Pattern.UNICODE_CHARACTER_CLASS);
                          Matcher m = p.matcher(w);
                          return m.matches();
                      }
                  

                  推荐答案

                  我怀疑你的文本字符中有类似于 non-breakable-space 不是空白,因此无法通过 \s 进行匹配.

                  I suspect that you have in your text character which is similar to non-breakable-space which is not white space so it can't be matched via \s.

                  在这种情况下,请尝试使用 p{Zs} 而不是 s.

                  In that case try to use p{Zs} instead of s.

                  如 http://www.regular-expressions.info/unicode.html 中所述

                  p{Zs} 将匹配任何类型的空格字符

                  p{Zs} will match any kind of space character

                  顺便说一句,如果您还想包含除空格之外的其他分隔符,例如制表符 或换行符 您可以组合p{Zs}s 类似 [p{Zs}s]

                  BTW if you would also like to include other separators than spaces like tabulators or line breaks you can combine p{Zs} with s like [p{Zs}s]

                  这篇关于Java 在字符串中看不到空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:可以将整数值分配给 char 但不能将整数变量分配 下一篇:如何检查一个字符是否等于一个空格?

                  相关文章

                  最新文章

                    <bdo id='Flmy0'></bdo><ul id='Flmy0'></ul>
                1. <i id='Flmy0'><tr id='Flmy0'><dt id='Flmy0'><q id='Flmy0'><span id='Flmy0'><b id='Flmy0'><form id='Flmy0'><ins id='Flmy0'></ins><ul id='Flmy0'></ul><sub id='Flmy0'></sub></form><legend id='Flmy0'></legend><bdo id='Flmy0'><pre id='Flmy0'><center id='Flmy0'></center></pre></bdo></b><th id='Flmy0'></th></span></q></dt></tr></i><div id='Flmy0'><tfoot id='Flmy0'></tfoot><dl id='Flmy0'><fieldset id='Flmy0'></fieldset></dl></div>
                2. <tfoot id='Flmy0'></tfoot>
                3. <small id='Flmy0'></small><noframes id='Flmy0'>

                      <legend id='Flmy0'><style id='Flmy0'><dir id='Flmy0'><q id='Flmy0'></q></dir></style></legend>