简体   繁体   English

java - 在输出中打印特殊字符和空格

[英]java - printing special characters and spaces in output

I have situation where I am reading from a file that contains special characters like ,$@%!我有一种情况,我正在从一个包含特殊字符的文件中读取,如 ,$@%! along with spaces.随着空间。 for example:例如:

I_am_here!!!我在这里!!! really,__am_I_here?真的,_我_我_在这里吗?

In the string above I am show a space with underscore just to show that I can come across multiple spaces (like two before am) as well as special characters.在上面的字符串中,我显示了一个带下划线的空格,只是为了表明我可以遇到多个空格(比如 am 之前的两个)以及特殊字符。 So I am just using _ as a visual aid.所以我只是使用 _ 作为视觉辅助。 In the real file it is actually a space or multiple spaces.在真实文件中,它实际上是一个空格或多个空格。

How can I read this file from input and also keep track of them.如何从输入中读取此文件并跟踪它们。 Is it possible to isolate the special characters and spaces and print them to the output along with the words read.是否可以隔离特殊字符和空格并将它们与读取的单词一起打印到输出。 I guess in short I want to preserve the special characters and spaces while modify the words that program reads.我想简而言之,我想在修改程序读取的单词的同时保留特殊字符和空格。

for example the input string:例如输入字符串:

I_am_here!!!我在这里!!! really,__am_I_here?真的,_我_我_在这里吗?

would become:会成为:

I_am_here!!!我在这里!!! really,__2_I_2?真的,__2_I_2?

Here I am reading the input string, counting the words that occur more than once and print out the output along with spaces and special characters that occurred in the original input.在这里,我正在读取输入字符串,计算出现多次的单词并打印输出以及原始输入中出现的空格和特殊字符。 How can I do this in Java?我怎样才能在 Java 中做到这一点? thanks谢谢

To find words in a string, the easiest is to use a regular expression.要在字符串中查找单词,最简单的方法是使用正则表达式。 The regex \\p{L}+ will find any sequence of one or more Unicode letters.正则表达式\\p{L}+将查找一个或多个 Unicode 字母的任何序列。

To replace such words with a value that isn't know until the word has been found, you can use the appendReplacement() and appendTail() methods of the Matcher used to find the words.要用直到找到单词才知道的值替换这些单词,您可以使用用于查找单词的MatcherappendReplacement()appendTail()方法。

Since you want to count how many times a word has been seen before, you can use a Map<String, Integer> to maintain a map of words already seen, and a count of how many times it has been seen so far.既然你想统计一个词之前被看过多少次,你可以使用Map<String, Integer>来维护一个已经看过的词的映射,以及到目前为止已经看过多少次的计数。

This is how it would work:这是它的工作方式:

String input = "I am here!!! really,  am I here?  Why, yes I really am!";

Map<String, Integer> wordCount = new HashMap<>();
StringBuffer buf = new StringBuffer();
Matcher m = Pattern.compile("\\p{L}+").matcher(input);
while (m.find()) {
    String word = m.group();
    Integer count = wordCount.get(word);
    if (count == null)
        wordCount.put(word, 1);
    else {
        wordCount.put(word, ++count);
        m.appendReplacement(buf, count.toString());
    }
}
String output = m.appendTail(buf).toString();

System.out.println(input);
System.out.println(output);

OUTPUT输出

I am here!!! really,  am I here?  Why, yes I really am!
I am here!!! really,  2 2 2?  Why, yes 3 2 3!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM