java - 使用全局变量的字符串到字符串数组转换
问题描述
我对java有以下问题。Class中的这个方法应该按原样返回String。
private String getAsString(Resource res) {
return "We wish you good luck in this exam!\nWe hope you are well pre-\npared.";
}
然后在构造函数中这个字符串被转换成单词数组
private int index;
private String string_arr[];
public TextFileIterator(Resource res) {
this.index=0;
if(res==null){
throw new NullPointerException();
}
String text=this.getAsString(res);
//text=text.replaceAll("-\n(?=[a-z])", "");
text=text.replaceAll("\\n", "");
text=text.replaceAll("!", " ");
text=text.replaceAll("-", "");
text=text.replaceAll(".", "");
this.string_arr=text.split(" ");
}
问题是最后我得到的数组是空的......有什么问题。我附上调试器屏幕截图。
请解释一下为什么会这样?
解决方案
罪魁祸首是第17行-
text=text.replaceAll(".", "");
上面的行是用“”替换所有内容,因为在正则表达式世界中“。” 表示任何字符。
试试这个——
text=text.replaceAll("\\.", "");
推荐阅读
- sql - 需要一个将数据存储为 Int 和 Decimal 的 DataType
- memory - Uwp SpeechSynthesizer/MediaPlayer 内存泄漏
- java - 包含返回 Mono 的函数时,我应该如何使用 Mono.zipWith?
- ios - 如何从扩展中的函数创建 UIActivityIndicator 计算属性
- ruby-on-rails - Rails ActiveRecord :pluck 和 :map 返回两个不同的值 - 为什么?
- python - 假设场景:Python 独立可执行文件以实现未来机器兼容性
- c# - 使用 IL2CPP 在 Visual Studio 中调试 Unity Hololens?
- java - C# 和 Java 服务器之间的 TCP 通信
- openssl - 如何使用 rust-openssl 创建与 `-starttls smtp` 等效的连接?
- java - Spring中类级别的多个requestMappings的Swagger-annotations