Java 如何从字符串中删除两个字符?
Java 如何从字符串中删除两个字符?,java,Java,NAI-PO-0009-1这是原始字符串。我需要删除-1,尤其是最后一个特殊字符和结束字符。我尝试过从字符串末尾删除两个字符,但未能成功。请执行以下操作: String test="NAI-PO-0009-1"; test=test.substring(0, test.length()-2); System.out.println(test); 输出: NAI-PO-0009 有关此类方法的更多信息,请访问。执行以下操作: String test="NAI-PO-0009-1"; test=
NAI-PO-0009-1
这是原始字符串。我需要删除-1
,尤其是最后一个特殊字符和结束字符。我尝试过从字符串末尾删除两个字符,但未能成功。请执行以下操作:
String test="NAI-PO-0009-1";
test=test.substring(0, test.length()-2);
System.out.println(test);
输出:
NAI-PO-0009
有关此类方法的更多信息,请访问。执行以下操作:
String test="NAI-PO-0009-1";
test=test.substring(0, test.length()-2);
System.out.println(test);
输出:
NAI-PO-0009
有关此类方法的更多信息,请访问。执行以下操作:
String test="NAI-PO-0009-1";
test=test.substring(0, test.length()-2);
System.out.println(test);
输出:
NAI-PO-0009
有关此类方法的更多信息,请访问。执行以下操作:
String test="NAI-PO-0009-1";
test=test.substring(0, test.length()-2);
System.out.println(test);
输出:
NAI-PO-0009
有关此类方法的更多信息,请访问。如果要在结束之前删除“-”的最后一个帐户,请执行以下操作:
String a="NAI-PO-0009-1";
System.out.println(a.substring(0, a.lastIndexOf('-'))-1);
如果您只想删除最后一个字符
System.out.println(a.substring(0, test.length()-2);
lastIndexOf
查找并返回字符的最后一次出现
substring
将子字符串返回到源字符串,该字符串以指定索引处的字符开头,并扩展到此字符串的结尾,如果给定第二个参数,则扩展到endIndex-1。如果要删除“-”的最后一个字符,直到结束,请执行以下操作:
String a="NAI-PO-0009-1";
System.out.println(a.substring(0, a.lastIndexOf('-'))-1);
如果您只想删除最后一个字符
System.out.println(a.substring(0, test.length()-2);
lastIndexOf
查找并返回字符的最后一次出现
substring
将子字符串返回到源字符串,该字符串以指定索引处的字符开头,并扩展到此字符串的结尾,如果给定第二个参数,则扩展到endIndex-1。如果要删除“-”的最后一个字符,直到结束,请执行以下操作:
String a="NAI-PO-0009-1";
System.out.println(a.substring(0, a.lastIndexOf('-'))-1);
如果您只想删除最后一个字符
System.out.println(a.substring(0, test.length()-2);
lastIndexOf
查找并返回字符的最后一次出现
substring
将子字符串返回到源字符串,该字符串以指定索引处的字符开头,并扩展到此字符串的结尾,如果给定第二个参数,则扩展到endIndex-1。如果要删除“-”的最后一个字符,直到结束,请执行以下操作:
String a="NAI-PO-0009-1";
System.out.println(a.substring(0, a.lastIndexOf('-'))-1);
如果您只想删除最后一个字符
System.out.println(a.substring(0, test.length()-2);
lastIndexOf
查找并返回字符的最后一次出现
substring
将子字符串返回到源字符串,该字符串以指定索引处的字符开头,并扩展到此字符串的结尾,或者如果给定第二个参数,则扩展到endIndex-1。如@Darshan Lila所说
String test="NAI-PO-0009-1";
test=test.substring(0, test.length()-2);
System.out.println(test);
用于删除最后两个字符。如果还需要删除最后一个特殊字符和后面的所有内容,则必须使用:
String test="NAI-PO-0009-1";
test=test.substring(0, test.lastIndexOf("-") -1);
/*explain: second parameter of SubString() method is for the length of the resulting
string, on a zero-based index, it has to end the last char before the last dash*/
System.out.println(test);
希望有帮助。就像@Darshan Lila说的那样
String test="NAI-PO-0009-1";
test=test.substring(0, test.length()-2);
System.out.println(test);
用于删除最后两个字符。如果还需要删除最后一个特殊字符和后面的所有内容,则必须使用:
String test="NAI-PO-0009-1";
test=test.substring(0, test.lastIndexOf("-") -1);
/*explain: second parameter of SubString() method is for the length of the resulting
string, on a zero-based index, it has to end the last char before the last dash*/
System.out.println(test);
希望有帮助。就像@Darshan Lila说的那样
String test="NAI-PO-0009-1";
test=test.substring(0, test.length()-2);
System.out.println(test);
用于删除最后两个字符。如果还需要删除最后一个特殊字符和后面的所有内容,则必须使用:
String test="NAI-PO-0009-1";
test=test.substring(0, test.lastIndexOf("-") -1);
/*explain: second parameter of SubString() method is for the length of the resulting
string, on a zero-based index, it has to end the last char before the last dash*/
System.out.println(test);
希望有帮助。就像@Darshan Lila说的那样
String test="NAI-PO-0009-1";
test=test.substring(0, test.length()-2);
System.out.println(test);
用于删除最后两个字符。如果还需要删除最后一个特殊字符和后面的所有内容,则必须使用:
String test="NAI-PO-0009-1";
test=test.substring(0, test.lastIndexOf("-") -1);
/*explain: second parameter of SubString() method is for the length of the resulting
string, on a zero-based index, it has to end the last char before the last dash*/
System.out.println(test);
希望能有所帮助。为什么
?应该是-
。我错了。谢谢,我改了为什么?应该是-
。我错了。谢谢,我改了为什么?应该是-
。我错了。谢谢,我改了为什么?应该是-
。我错了。谢谢,我改了