Results 1 to 10 of 10
- 02-22-2009, 08:10 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 3
- Rep Power
- 0
combine string[] into string like perl's join function
I'm looking for a way to combine elements of a string array (or object.toString()s) into a single string. More specifically is there is a Java method or simple combination of methods, that can perform this kind of join in a single expression?
I'm not looking for this:
public static String join(String delimiter, Object[] array) {
StringBuilder sb = new StringBuilder();}
boolean firstTime = true;
for(Object element: array) {
if (firstTime)}
firstTime = falseelse
sb.append(delimiter);sb.append(element.toString());
return sb.toString();
I'm looking for something like this:
String foo = array.join(",");
I've looked in Java 6's Collections, Arrays and String classes with no luck.
--tom
- 02-22-2009, 11:13 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
As far as I know there is no simple way to add element into a single string object. Easiest way to do this is, loop through the array and concat each element of the array in to previous one.
- 02-22-2009, 03:02 PM #3
String array to string
Maybe something like this:
Java Code:String daString; for (int i = 0; i < daArray.length; i++) { daString += daArray[i]; }
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-22-2009, 05:31 PM #4
Member
- Join Date
- Feb 2009
- Posts
- 3
- Rep Power
- 0
The simplest example of what I'm looking for is this:
XML Code:public static void main(String[] args) { final String[] array = new String[]{"one", "two", "three", "four"}; String s = new Scanner( new Readable() { private int i = 0; public int read(CharBuffer ch) { return (i < array.length) ? ch.append(array[i++]).length() : -1; } } ).useDelimiter("\\Z").next(); System.out.println(s); }
The problems are that the string array has to be final, and it is hardcoded in the read method.
- 02-22-2009, 09:23 PM #5
uh, no...
Not sure how you can get an output, because the above code doesn't compile.
Java Code:String s = new Scanner(...
I'm not sure what you want to do and I'm not sure what the problem is. Maybe you could try to explain again. I also don't see the relationship between the initial question and the posted code.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-22-2009, 09:47 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 3
- Rep Power
- 0
You have to look at the end of the expression to see how a String is generated. There are 3 expressions nested together. First a Scanner object is created. Second the 'useDelimiter' method sets the delimiter to EOF and returns the same Scanner object. Third the 'next' method scans the whole thing and returns a String.
- 02-22-2009, 10:15 PM #7
but how is this simpler than CJSLMAN's example?
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-22-2009, 10:20 PM #8
Oh... OK... the way it's coded, I didn't catch that. For some crazy and unknown reason, the complier was giving me a couple of errors. I saved the program again and it worked (go figure). and yes I get your result as an output.
Sorry, but I still don't understand what the problem is. You want the output to be "one two three four" ?
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-23-2009, 06:09 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 26
- 02-23-2009, 01:05 PM #10
Similar Threads
-
how use string array while passing to a function
By sks9s9 in forum New To JavaReplies: 2Last Post: 02-03-2009, 12:44 PM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
String substring function
By ravian in forum New To JavaReplies: 6Last Post: 01-02-2008, 07:35 PM -
Using java.util.Scanner to search for a String in a String
By Java Tip in forum Java TipReplies: 0Last Post: 11-20-2007, 04:59 PM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM
Bookmarks