从 Java 中的字符串中删除最后一个字符
Deleting Last Character From a String in Java
我正在编写一个 java 代码,当在 PasswordField 中输入超过 10 个字符时会出错,并且无法从那里输入。我尝试在 KeyPressed 事件中从 PasswordField 中删除最后一个字符,但不是删除最后一个字符,而是删除它之前的字符并将其替换为最后一个 character.Here 我的代码。
private void passFieldKeyTyped(java.awt.event.KeyEvent evt) {
String pw1 = new String(passField.getPassword());
if(passField.getPassword().length==10){
try{
StringBuffer bf = new StringBuffer(pw1);
bf.deleteCharAt(10);
String pw2 = new String(bf);
passField.setText(pw2);
JOptionPane.showMessageDialog(this, "<html><h4>Password Must Not Contain More Than 10 Characters !</h4></html>", "Error !", JOptionPane.ERROR_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
我还是编程新手。我希望有人可以帮助我。谢谢!
public class RemoveChar {
public static void main(String[] args) {
String str = "India is my country";
System.out.println(charRemoveAt(str,));
}
public static String charRemoveAt(String str) {
return str.substring(0, str.length()-1);
}
}
我猜您正试图从字符串中删除一个字符。
您可以在这里使用StringBuilder class删除指定位置的字符。首先将您的字符串转换为 StringBuilder -
StringBuilder sb = new StringBuilder(inputString);
然后你可以使用内置方法deleteCharAt()
删除你想要的位置的字符,就像这样 -
sb.deleteCharAt(10);
希望对您有所帮助。
伙计们,很抱歉,我发现这是我的错误。实际上我不得不再次检查密码字段的长度,长度必须为 11 才能删除第 10 个字符。 code.It 成功了..感谢大家的帮助 !!!!!!!!
private void passFieldKeyTyped(java.awt.event.KeyEvent evt) {
String pw1 = new String(passField.getPassword());
if(passField.getPassword().length==11){
try{
StringBuffer bf = new StringBuffer(pw1);
bf.deleteCharAt(10);
String pw2 = new String(bf);
passField.setText(pw2);
JOptionPane.showMessageDialog(this, "<html><h4>Password Must Not Contain More Than 10 Characters !</h4></html>", "Error !", JOptionPane.ERROR_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
我正在编写一个 java 代码,当在 PasswordField 中输入超过 10 个字符时会出错,并且无法从那里输入。我尝试在 KeyPressed 事件中从 PasswordField 中删除最后一个字符,但不是删除最后一个字符,而是删除它之前的字符并将其替换为最后一个 character.Here 我的代码。
private void passFieldKeyTyped(java.awt.event.KeyEvent evt) {
String pw1 = new String(passField.getPassword());
if(passField.getPassword().length==10){
try{
StringBuffer bf = new StringBuffer(pw1);
bf.deleteCharAt(10);
String pw2 = new String(bf);
passField.setText(pw2);
JOptionPane.showMessageDialog(this, "<html><h4>Password Must Not Contain More Than 10 Characters !</h4></html>", "Error !", JOptionPane.ERROR_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
我还是编程新手。我希望有人可以帮助我。谢谢!
public class RemoveChar {
public static void main(String[] args) {
String str = "India is my country";
System.out.println(charRemoveAt(str,));
}
public static String charRemoveAt(String str) {
return str.substring(0, str.length()-1);
}
}
我猜您正试图从字符串中删除一个字符。
您可以在这里使用StringBuilder class删除指定位置的字符。首先将您的字符串转换为 StringBuilder -
StringBuilder sb = new StringBuilder(inputString);
然后你可以使用内置方法deleteCharAt()
删除你想要的位置的字符,就像这样 -
sb.deleteCharAt(10);
希望对您有所帮助。
伙计们,很抱歉,我发现这是我的错误。实际上我不得不再次检查密码字段的长度,长度必须为 11 才能删除第 10 个字符。 code.It 成功了..感谢大家的帮助 !!!!!!!!
private void passFieldKeyTyped(java.awt.event.KeyEvent evt) {
String pw1 = new String(passField.getPassword());
if(passField.getPassword().length==11){
try{
StringBuffer bf = new StringBuffer(pw1);
bf.deleteCharAt(10);
String pw2 = new String(bf);
passField.setText(pw2);
JOptionPane.showMessageDialog(this, "<html><h4>Password Must Not Contain More Than 10 Characters !</h4></html>", "Error !", JOptionPane.ERROR_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());
}
}