How to join a range of array string slots with blank delimiters? Almost opposite of string.split()?

G

Gunter Hansen

Assume I split a text line with a command like:

String[] part = line.split("\\s+");

Ok, fine, but how is the opposite command?
Keep in mind that the delimiter between the joined slots of the target string should be one blank.

Moreover: Can I join e.g. only slots [3],...,[MAX]?

So I need a command similar to:

String joined = part[3,MAX].join(' ');

How ca I achieve this?

If this is not possible directly then maxbe with a work around?

Gunther
 
R

Roedy Green

If this is not possible directly then maxbe with a work around?

I would do it like this:

final FastCat sb = new FastCat ( slots.length * 2 - 1 );
for ( String slot ; slots )
{
if( sb.length() != 0 )
{
sb.append( ' ' );
}
sb.append( slot );
}
final String joined = sb.toString();

see http://mindprod.com/products1.html#FASTCAT

Alternatively you could compute the final string length and allocate a
char[]. Then plop the pieces in, and convert to a string, which is
roughly what FastCat does under the hood.

--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 
A

Arne Vajhøj

Assume I split a text line with a command like:

String[] part = line.split("\\s+");

Ok, fine, but how is the opposite command?
Keep in mind that the delimiter between the joined slots of the target string should be one blank.

You can always use a StringBuilder and a loop.

If you like weird one liners try:

line = Arrays.toString(part).replaceAll("[\\[\\],]", "");
Moreover: Can I join e.g. only slots [3],...,[MAX]?

So I need a command similar to:

String joined = part[3,MAX].join(' ');

How ca I achieve this?

StringBuilder and a loop.

Arne
 
A

Arne Vajhøj

Assume I split a text line with a command like:

String[] part = line.split("\\s+");

Ok, fine, but how is the opposite command?
Keep in mind that the delimiter between the joined slots of the target
string should be one blank.

Moreover: Can I join e.g. only slots [3],...,[MAX]?

So I need a command similar to:

String joined = part[3,MAX].join(' ');

How ca I achieve this?

If this is not possible directly then maxbe with a work around?

I am surprised the feature doesn't exist in the JDK; in .NET there's a
String.Join() method that does exactly what you're asking about. But
AFAIK, it doesn't.

Even split was just added in 1.4 - before that it was StringTokenizer.
And I have to admit, I think Java's take on the "split" function is
pretty cool, what with regex support and all (which .NET doesn't
provide, not as a String.Split() feature anyway).

..NET has Regex.Split.

Arne
 
R

Roedy Green

for ( String slot ; slots )

oops that should read

for ( String slot : slots )
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top