Click to See Complete Forum and Search --> : Spliting a String


Pankaj Kumar
09-08-2008, 03:32 AM
Hi

I am trying to split a string on ":" . Below is the snippet of the code

System.out.println("User Info Test--->"+userDetails.get(2).toString().split("[:]").length);
System.out.println("User Info Test--->"+userDetails.get(2).toString().split("[:]"));

where userDetails is a Array List Object. I am getting output as

"givenName: Fedrik"
I want to get rid of "givenName: " in my output. Can anybody help me.

Regards

chazzy
09-08-2008, 03:23 PM
why are you using [:] instead of just : ?

Pankaj Kumar
09-09-2008, 12:56 AM
Well I tried that too.....but no luck there too.

Will appreciate you send me some code.

Regards

Pankaj Kumar
09-09-2008, 01:02 AM
Well I tried that too.....but no luck there too.

Will appreciate you send me some code.

Regards

kurent
09-09-2008, 04:25 AM
var slice_separator = userDetails.get(2).toString().indexOf(":");
var resulting_string = userDetails.get(2).toString().slice(slice_separator+2);


+2 because we want to skip ": "
The slice function will get everything until the end of the string if the second parameter is not specified. You could do userDetails.get(2).toString().slice(slice_separator+2, userDetails.get(2).toString().length); but some browsers have a problem with the length or something like that. Try whatever works.

Khalid Ali
09-09-2008, 06:13 PM
I am not sure why you say it doesnt work. However just couple of things here,
String.split() function returns an array not a string, therefore you have to access the value you are interested in by its index in the array.

Here is the code snippet that works.


List userDetails = new ArrayList();
userDetails.add(new String("givenName:Khalid"));
userDetails.add(new String("givenName:Bob"));
userDetails.add(new String("givenName:Frederik"));
userDetails.add(new String("givenName:Jon"));

String name = userDetails.get(2).toString().split(":")[1];
System.out.println(name);


Hope this helps

chazzy
09-09-2008, 06:42 PM
I think you're using javascript.

Khalid Ali
09-09-2008, 09:45 PM
doesnt look like it (unless he doesnt know what he is using)..:-)

kurent
09-10-2008, 02:04 AM
He is right, I was thinking JavaScript hehe. :o

kurent
09-10-2008, 02:05 AM
*double post*

Khalid Ali
09-10-2008, 06:52 AM
nevermind, I was referring to the original posters question

Pankaj Kumar
09-11-2008, 12:38 AM
Thanks to all of you. I was missing index in my code.