例如,我正在从文本文件中提取一个文本字符串,并且需要将这些单词组成一个数组。然而,当我这样做时,一些单词以逗号(,)、句号(.)甚至带有括号(这是完全正常的)结束。
我想要做的是去掉这些字符。我一直在尝试使用Java中那些预定义的String方法来实现,但我就是无法解决它。
我想要做的是去掉这些字符。我一直在尝试使用Java中那些预定义的String方法来实现,但我就是无法解决它。
将变量重新赋值为子字符串:
s = s.substring(0, s.length() - 1)
还有一种解决你问题的替代方法:你可能想考虑使用StringTokenizer来读取文件,并将分隔符设置为不希望成为单词一部分的字符。
使用:
String str = "whatever";
str = str.replaceAll("[,.]", "");
replaceAll方法使用正则表达式。这是一个例子:
[,.]
...查找每个逗号和/或句点。
按照Mark Byers所说,要删除最后一个字符
s = s.substring(0, s.length() - 1);
.replace(oldCharacter, newCharacter)
方法。例如:s = s.replace(",","");
并且
s = s.replace(".","");
在Java中,您无法修改字符串。它们是不可变的。您所能做的就是创建一个新字符串,该字符串是旧字符串的子字符串,减去最后一个字符。
在某些情况下,StringBuffer可能会更好地帮助您。
if ((textArray[i].endsWith(",")) || textArray[i].endsWith("."))
textArray[i].substring(textArray[i].indexOf(textArray[i].length()-1));
System.out.println(textArray[i].toLowerCase());
}
[/code] - Slavisa Perisics = s.substring(0, s.length() - 1)
String.replaceAll("\\", "");
String.replaceAll("\\$", ""); //if it is a path
import java.text.BreakIterator;
import java.util.Locale;
public static void main(String[] args) {
String text = "\n" +
"\n" +
"For example I'm extracting a text String from a text file and I need those words to form an array. However, when I do all that some words end with comma (,) or a full stop (.) or even have brackets attached to them (which is all perfectly normal).\n" +
"\n" +
"What I want to do is to get rid of those characters. I've been trying to do that using those predefined String methods in Java but I just can't get around it.\n" +
"\n" +
"Every help appreciated. Thanx";
BreakIterator wordIterator = BreakIterator.getWordInstance(Locale.getDefault());
extractWords(text, wordIterator);
}
static void extractWords(String target, BreakIterator wordIterator) {
wordIterator.setText(target);
int start = wordIterator.first();
int end = wordIterator.next();
while (end != BreakIterator.DONE) {
String word = target.substring(start, end);
if (Character.isLetterOrDigit(word.charAt(0))) {
System.out.println(word);
}
start = end;
end = wordIterator.next();
}
}
来源:http://java.sun.com/docs/books/tutorial/i18n/text/word.html
replaceAll()
方法:String.replaceAll(",", "");
String.replaceAll("\\.", "");
String.replaceAll("\\(", "");
等等……
String.substring()
的源代码即可。 - Stephen C