英文:
String to String array conversion with global variables
问题
以下是您提供的内容的翻译:
我在使用Java时遇到了以下问题。在类中,这个方法应该原样返回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(" ");
}
问题是,最后我得到了一个为空的数组...问题出在哪里。我附上了调试器的截图。
请问您能解释一下为什么会发生这种情况吗?
英文:
I have following problem with java.This method in Class should return String as is.
private String getAsString(Resource res) {
return "We wish you good luck in this exam!\nWe hope you are well pre-\npared.";
}
Then in Constructor this String shlud be converted into array of words
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(" ");
}
Problem is that at the end I get array which is null... what is the problem. I attach the debugger screenshots.
Please could explain me why does it happen?
答案1
得分: 3
罪魁祸首是第17行-
text=text.replaceAll("。", "");
上述行将所有内容替换为"",因为在正则表达式世界中,"."代表任意字符。
请尝试改用以下方式-
text=text.replaceAll("\\。", "");
英文:
The culprit is line no 17-
text=text.replaceAll(".", "");
The above line is replacing all of the content with "", because in regex world "." means any character.
Try this instead-
text=text.replaceAll("\\.", "");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论