Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Convert from String Array to String


  • From: stu@xxxxxxxxxxx (Stuart Halloway)
  • Subject: Convert from String Array to String
  • Date: Mon, 30 Oct 2000 21:40:25 -0500

>     Is there a way to convert from String Array to String??
> 	(...)
>     I find that I have to use a for loop to convert, is there any standard
> method that does this??
>
>     for (int i = 0; i < usernameArr.length; i++)
>     {
>             usernameStr += usernameArr[i];
>     }

A loop is how you do it.  However, you should _not_ assign to a String
reference each time through the loop, as this will create lots of unneeded
String objects.  Instead, use a StringBuffer.  To be even more
performance-oriented, don't access the length field every time through, and
try to preallocate the buffer capacity.  Putting that all together:

StringBuffer buf = new StringBuffer(GOOD_GUESS_OF_SIZE);
int length = usernameArr.length;
for (int i = 0; i < length; i++)
{
  buf.append(usernameArr[i]);
}
username = buf.toString();

Stuart Halloway
DevelopMentor
http://staff.develop.com/halloway


---
To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx
To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx