简体   繁体   English

我想删除字符串的第一个字符

[英]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.

相关问题 我想检查字符串的空格,并想在空格后将第一个字符打印为大写字母 - I want to check the whitespace of a string and want to print the first character just after the whitespace to a upper case letter 我想打印字符串数组中第一个字符串的第一个字符和该数组中最后一个字符串的最后一个字符 - I want to print the first character of the first string from an array of Strings and the last character from the last string from that Array 我想知道如何在字符串中查找特殊字符 - I want to know how to find a special character in a string 我想以一个字符只重复一次的方式修剪一个字符串 - I want to trim a string in a way that a character will repeat only once 我要为输入字符串的用户中的每个字符显示字母“ z” - I want to display letter 'z' for each character in a String user enters 字符串的第一个和最后一个字符 - First and Last Character of a String 为什么当我想从文本文件中删除字符串时它不起作用? - Why when I want to delete a String from a textfile it does not work? 如何确定字符串的第一个字符是否为数字? - How do I find out if first character of a string is a number? 如何打印字符串中第一个字符的二进制表示 - How do I print the binary representation of the first character in a string 如何检查字符串是否带有+,-或。 (十进制)的第一个字符? - How can I check if a string has +, -, or . (decimal) in the first character?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM