Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Splitting a String  RSS feed

 
Rohan Ramakumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I want to split the following string :

'String st = 12&45&&56&7&&'

And I want the output as :

12
45
<----- white space
56
7
<---- white space

However, when I put st.split("&",-1)

I get the following

12
45
<------- white space
56
7
<------- white space
<------- white space

That is I'm getting an additional white space at the end. Why is it so and what should I do to get desired output?


Thanks
 
Hira Iqbal
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replace -1 with 0

Regards
Hira
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that excludes all trailing empty Strings, which wasn't the requirement.

Split on ("&(&$)?", -1)
 
Rohan Ramakumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Darryl ,

Thanks for your reply. It's working properly.

Thanks
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome, but do you understand why it works for your requirement?
 
Rohan Ramakumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I did not notice it earlier but there is an issue.

Now it is adding blank space at the end of all strings (i.e) even if I have string like '12&45&&56&7&' , I'm getting :

12
45
<----- white space
56
7
<----- white space

But I need the output as :

12
45
<----- white space
56
7


Thanks
 
Rohan Ramakumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did the following and it's working properly now :

st=st.substring(0,st.lastIndexOf("&"));
st.split("&(&$)?",-1);

However, I want to know how to get desired output using split alone.

Thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohan Ramakumar wrote:I did the following and it's working properly now :

st=st.substring(0,st.lastIndexOf("&"));
st.split("&(&$)?",-1);

Be careful - that strips off the last "&" and everything after it. If your string is "12&45&&56&7", for instance, you'll lose the 7 completely.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know how to get desired output using split alone.

You can't. If the input ends with a single (not double) ampersand, either you split on it or you don't.
If you split on it, there'll be a trailing empty String.
If you don't, the last element of the split will contain the ampersand.

st=st.substring(0,st.lastIndexOf("&"));

What does that do if the input doesn't end with an ampersand at all? e.g. for the input "12&45&&56&7"

edit Matthew beat me by 7 seconds!
 
Rohan Ramakumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I forgot to mention this earlier. This is what I'm actually doing... I read the cell contents of a row using POI and after every cell content I append an ampersand. Later on I strip the ampersand, so basically the string will always have an ampersand and end with it.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't use POI so this may seem like a silly question... can't you read the cell contents directly into a String[], or if the number of cells is not known at the start of reading, into a List<String>?
 
Rohan Ramakumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can read from cells directly...But I have a requirement for which appending the ampersand becomes necessary.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!