[英]I want to delete the first character of a string
I have very very silly question but it is taking me a long time and I really don't know what the problem is. 我有一个非常非常愚蠢的问题,但是这花了我很长时间,我真的不知道问题出在哪里。 Take a look to my code please: 请看一下我的代码:
String PT = myedittext.getText().toString();
int len = PT.length();
char chars[] = PT.toCharArray();
for (int i = 0; i < len; i++)
chars[i] = chars[i++];
As you can see, I want to delete the first character but it is not working and after this code my array is the same before. 如您所见,我想删除第一个字符,但是它不起作用,并且在此代码之后,我的数组之前相同。
I'm going crazy. 我要疯了。 What is the error? 有什么错误?
如果要摆脱第一个字符,只需使用substring()
:
String newString = PT.substring(1);
change this: 改变这个:
for (int i = 0; i < len; i++)
chars[i] = chars[i++];
to this: 对此:
for (int i = 0; i < len-1; i++)
chars[i] = chars[i+1];
the result string will be: 结果字符串将是:
Arrays.toString(chars);
also if you don't need the char array and now that you know the problem of your code substring
would be a better alternative. 同样,如果您不需要char数组,并且现在您知道代码substring
的问题将是更好的选择。
The problem occurs here: 问题发生在这里:
chars[i] = chars[i++];
This is the same as: 这与:
chars[i] = chars[i];
i = i + 1;
For removing the first character you should use substring: 要删除第一个字符,应使用子字符串:
PT.substring(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.