views:

63

answers:

2

Hello,

Recently I asked this question: http://stackoverflow.com/questions/3199449/removing-up-to-4-spaces-from-a-string

and it works just fine. I am interested however in counting the number of spaces I have removed as well. How can I do this? Count the number of spaces removed using this?

stringArray[i] = stringArray[i].replaceFirst ("^ {0,4}", "");

Basically, I need to be able to remove up to 4 spaces from a string, and then save in an int how many spaces were actually removed. Any help on this would be great!

Thanks!

+3  A: 

Here's one simple way:

int oldLen = stringArray[i].length();
stringArray[i] = stringArray[i].replaceFirst ("^ {0,4}", "");
int spacesRemoved = oldLen - stringArray[i].length();
mattmc3
`spacesRemoved` will always end up `0` or negative using this code.
LukeH
Good catch. Fixed.
mattmc3
A: 
int oldLength = stringArray[i].length();
stringArray[i] = stringArray[i].replaceFirst("^ {0,4}", "");
int spacesRemoved = oldLength - stringArray[i].length();
LukeH