我想从字符串中删除最后一个字符。我试过这样做:
public String method(String str) {
if (str.charAt(str.length()-1)=='x'){
str = str.replace(str.substring(str.length()-1), "");
return str;
} else{
return str;
}
}
获取字符串的长度- 1,并将最后一个字母替换为空(删除它),但每次我运行程序时,它都会删除与最后一个字母相同的中间字母。
例如,单词是“仰慕者”;在我运行这个方法之后,我得到了“钦佩”。我想让它回复“钦佩”这个词。
为什么不只是一条航线呢?
public static String removeLastChar(String str) {
return removeLastChars(str, 1);
}
public static String removeLastChars(String str, int chars) {
return str.substring(0, str.length() - chars);
}
完整代码
public class Main {
public static void main (String[] args) throws java.lang.Exception {
String s1 = "Remove Last CharacterY";
String s2 = "Remove Last Character2";
System.out.println("After removing s1==" + removeLastChar(s1) + "==");
System.out.println("After removing s2==" + removeLastChar(s2) + "==");
}
public static String removeLastChar(String str) {
return removeLastChars(str, 1);
}
public static String removeLastChars(String str, int chars) {
return str.substring(0, str.length() - chars);
}
}
Demo
if (str.endsWith("x")) {
return str.substring(0, str.length() - 1);
}
return str;
例如,单词是“仰慕者”;在我运行这个方法之后,我得到了“钦佩”。我想让它回复“钦佩”这个词。
以防你想学英语单词
词干化是将屈折变化的(有时是派生的)单词还原到词干、词根或词根形式的过程,通常是书面的单词形式。
...
例如,英语的词干器应该识别基于词根“cat”的字符串“cats”(可能还有“catlike”、“catty”等),以及基于“stem”的字符串“stemmer”、“stem”、“”。词干算法将单词“fishing”、“fishing”、“fish”和“fisher”简化为词根单词“fish”。
Lucene stemmer的区别:EnglishStemmer, PorterStemmer, LovinsStemmer概述了一些Java选项。
// Remove n last characters
// System.out.println(removeLast("Hello!!!333",3));
public String removeLast(String mes, int n) {
return mes != null && !mes.isEmpty() && mes.length()>n
? mes.substring(0, mes.length()-n): mes;
}
// Leave substring before character/string
// System.out.println(leaveBeforeChar("Hello!!!123", "1"));
public String leaveBeforeChar(String mes, String last) {
return mes != null && !mes.isEmpty() && mes.lastIndexOf(last)!=-1
? mes.substring(0, mes.lastIndexOf(last)): mes;
}
我不得不为类似的问题编写代码。我解决这个问题的一种方法是用递归的编码方法。
static String removeChar(String word, char charToRemove)
{
for(int i = 0; i < word.lenght(); i++)
{
if(word.charAt(i) == charToRemove)
{
String newWord = word.substring(0, i) + word.substring(i + 1);
return removeChar(newWord, charToRemove);
}
}
return word;
}
我所见过的关于这个主题的大多数代码都没有使用递归,所以希望我可以帮助您或有相同问题的人。
如何在最后的递归中创建char:
public static String removeChar(String word, char charToRemove)
{
String char_toremove=Character.toString(charToRemove);
for(int i = 0; i < word.length(); i++)
{
if(word.charAt(i) == charToRemove)
{
String newWord = word.substring(0, i) + word.substring(i + 1);
return removeChar(newWord,charToRemove);
}
}
System.out.println(word);
return word;
}
为例:
removeChar ("hello world, let's go!",'l') → "heo word, et's go!llll"
removeChar("you should not go",'o') → "yu shuld nt goooo"
这个众所周知的问题在Perl中通过chop()和chomp()完美地解决了。
这里有一个很长的答案:Java chop and chomp
代码如下:
//removing trailing characters
public static String chomp(String str, Character repl) {
int ix = str.length();
for (int i=ix-1; i >= 0; i-- ){
if (str.charAt(i) != repl) break;
ix = i;
}
return str.substring(0,ix);
}
//hardcut
public static String chop(String str) {
return str.substring(0,str.length()-1);
}