I have a very simple question, in fact I’m a bit frustrated that I can’t solve this on my own but here it is:
strBuffer += arg.charAt( i );
With this line I’m trying to parse a value and add it character by character to a new string. I’m doing this to seperate a single long string into an array of smaller string.
Example, this string
-time delayed -run auto -mode go_n_run
would become this array
strBuffer [0] = -time
strBuffer [1] = delayed
strBuffer [2] = -run
strBuffer [3] = auto
strBuffer [4] = -mode
strBuffer [5] = go_n_run
So the line of code with the ‘+=’ doesn’t work, nothing gets put in my strBuffer. So I’ve tried something a little more “complex” that I found on a forum :
strBuffer.concat( new String( new char[]{arg.charAt( i )} ) );
But same result, nothing is put in strBuffer,
So, any hints would be appreciated
Thanks
EDIT : Here is the complete method
String[] args = new String[2 * ARG_LIMIT];
int index = 0;
for( int i = 0; i < arg.length(); i++ )
{
String strBuffer = new String();
if( arg.charAt( i ) != ' ' )
{
// The two methods I've tried
strBuffer.concat( new String( new char[]{arg.charAt( i )} ) );
strBuffer += arg.charAt( i );
}
else if( arg.charAt( i ) == ' ' )
{
args[index] = strBuffer;
index++;
strBuffer = "";
}
}
I will assume that
strBufferis an instance of java’sStringBuffer; if so – you should usestrBuffer.append().But there’s a way simpler method for doing the thing you want: