【发布时间】:2016-04-19 16:30:48
【问题描述】:
我正在尝试为 TextFiles 制作压缩器,但在替换字符时遇到了困难。
这是我的代码:
compress.setOnAction(event ->
{
String line;
try(BufferedReader reader = new BufferedReader(new FileReader(newFile)))
{
while ((line = reader.readLine()) != null)
{
int length = line.length();
String newLine = "";
for (int i = 1; i < length; i++)
{
int c = line.charAt(i);
if (c == line.charAt(i - 1))
{
}
}
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
});
所以我想做的是:我想找到两个字符相等的所有单词,如果它们在旁边(比如“Took”)。当 if 语句为真时,我想替换两个等号字符的第一个字母,所以它看起来像:'T2ok'。
我已经尝试了很多东西,我总是得到一个 ArrayOutOfbounds、StringOutOfbounds 等等......
希望有人有一个很好的答案:-)
问候
【问题讨论】:
-
Java 中的字符串是不可变对象,因此:您的问题的答案是使用
StringBuilder而不是String。 Check this answer 了解更多信息。 -
对标题中的 Alice 部分感到好奇,那是什么意思?