I’m having more and more trouble while trying to make an easy excercise.
i’m trying to delete all eventual starting charater ” ” in a string.
Example:space-space-space50(i cannot figure it because this forum remove extra space)
result:50
Here’s the code …
char c=textToShow.getText().toString().charAt(1);
int i=1;
while (c == ' '){
textToShow.setText(textToShow.getText().toString().substring(i));
//New char
i++;
c=textToShow.getText().toString().charAt(i);
}
You have the right idea, but your implementaion is off.
First
should start at
0, that is how the indexes are made.The int is properly initialized but is not even really needed. This is because you work with only the beginning of the String.
However your loop is where things go awry.
Your substring code is proper, but now, you don’t need to increment i, since you’re modifying the String by shortening it. Also,
Should be c=textToShow.getText().toString().charAt(0); You are still checking the beginning of the String.
A more efficient implementation using the loop could be:
Of course, this is for an excersice, if you wanted to do it the right way, you’d use temp.trim();