Spliting String on space

Highlighted
Four Stars

Spliting String on space

Hi, if someone could please help me, i'm trying to split a string like this:

String s1=input_row.m_lines;
int pos=s1.indexOf(":");

context.Line=s1.substring(0,pos) ; 
context.Mask=s1.substring(pos+1) ; 

String str = s1.substring(0,pos) ;
String[] splited = str.split("\\s+");

//System.out.println(context.Line);
System.out.println(splited);

and it gives me this:

[statistics] connecting to socket on port 3496
[statistics] connected
[Ljava.lang.String;@2669b199

[Ljava.lang.String;@2344fc66

[Ljava.lang.String;@458ad742
[statistics] disconnected

The output of System.out.println(context.Line) without the split function is:

Name

Surname

Email

i want to split all and store each in different variables, anyways to do so?


Accepted Solutions

Re: Spliting String on space

Hi,

 

please note that you do not print out the content of your Array after split.

 << System.out.println(splited);

To print out a value stored in the array you would acces it like : System.out.println(splited[0]);

 

See also following snippet and try it out :

String s1="Hello my dear I do have something to say I hope you gonna like it";

String[] splited = s1.split("\\s+");

//System.out.println(context.Line);
System.out.println(splited);

for(String w:splited){
System.out.println(w);
}

The output will be :

 

 [Ljava.lang.String;@7852e922
Hello
my
dear
I
do
have
something
to
say
I
hope
you
gonna
like
it

----------------------------

I hope it helps

BR B_p

View solution in original post


All Replies

Re: Spliting String on space

Hi,

 

please note that you do not print out the content of your Array after split.

 << System.out.println(splited);

To print out a value stored in the array you would acces it like : System.out.println(splited[0]);

 

See also following snippet and try it out :

String s1="Hello my dear I do have something to say I hope you gonna like it";

String[] splited = s1.split("\\s+");

//System.out.println(context.Line);
System.out.println(splited);

for(String w:splited){
System.out.println(w);
}

The output will be :

 

 [Ljava.lang.String;@7852e922
Hello
my
dear
I
do
have
something
to
say
I
hope
you
gonna
like
it

----------------------------

I hope it helps

BR B_p

View solution in original post

Four Stars

Re: Spliting String on space

Thanx, it actually solved it ^^

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog