• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is there a way to Split a string then take that split value and split it again?

 
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm just practicing String.Split at the moment , and I find examples for String.Split but I thought what if I want to split the split results ?  I am not figuring out a way to do this and no examples.  
I was thinking one of my issues is after I split the string the first time, it creates an array.   If I want to split one array result it works.  
But what if I want to loop through each part of the array result ( result from the first split ) and further split those Strings?





But what if I want to loop through the entire array and split each of those Strings?  I tried a double array but received an error.




 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You overlooked 2 things.

1. line 6. firstSplit is an array, not the string, so you need to split its element by accessing some in particular. i.e. firstSplit[x].split(...)
2. line 2. you don't know what the length going to be of the inner array as a result after the split. So you don't need to specify this length.

After fixing those 2 parts I get it working.
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:You overlooked 2 things.

1. line 6. firstSplit is an array, not the string, so you need to split its element by accessing some in particular. i.e. firstSplit[x].split(...)
2. line 2. you don't know what the length going to be of the inner array as a result after the split. So you don't need to specify this length.

After fixing those 2 parts I get it working.



I am sorry if I missed something obvious here but I receive a Type mismatch when I try that.

 
Saloon Keeper
Posts: 15491
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use Pattern.splitAsStream() in conjunction with Stream.flatMap():
 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You can also use Pattern.splitAsStream() in conjunction with Stream.flatMap():



Thank you for this.  I need to learn Pattern it seems like.      This did work really well but I guess I should give up on the idea of splitting a split string?   It's was only my attempt to practice and learn anyways.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lisa Austin wrote:I am sorry if I missed something obvious here but I receive a Type mismatch when I try that.


Should be
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And secondSplitOption1 outter array needs to be initialised while inner not.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To print it to see if it worked

 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works!  Thank You.

But I had to go back to how I first initialized my secondSplitOption1 array.   So I'm not clear regarding the instructions about initializing secondSplitOption1.  My original code I use the firstSplit.length and when I went back to doing it this way it worked for me.  Can I please get clarification on what was trying to be communicated?  



 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to look very carefully at the initializer I wrote. It's different than yours.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Here I'm allocating  space for N references to String arrays but I'm not declaring how many Strings will be in the String array nor am I allocating references to any String array at this point. That comes later in the loop.

 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Carey Brown wrote:Here I'm allocating  space for N references to String arrays but I'm not declaring how many Strings will be in the String array nor am I allocating references to any String array at this point. That comes later in the loop.



Oh geeze I'm blind.  Thank You!  

So silly question.  I'm just trying to challenge myself because I really , really want to practice the tutorials I find.    In regards to splitting an already split string.  If I had ever needed this,  Would using the Pattern class be a better solution?    
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This case does not lend itself well to regular expressions. I've implement similar code many times in production code.
 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:This case does not lend itself well to regular expressions. I've implement similar code many times in production code.



Can you share with me how you would do it?    If it's not too much trouble.
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Essentially the same way you've done it except with some business logic sprinkled in.
 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Essentially the same way you've done it except with some business logic sprinkled in.



Thank You!   Thanks everyone for your help.   Now to see what I can do with what I learned.  
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a simple example, using this technique to create a map of argument name-value pairs from a URL.

Output:
 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Here's a simple example, using this technique to create a map of argument name-value pairs from a URL.

Output:




Thank You!  I'm going to play with this and see what all I can do with this.  I appreciate it.  I practice on some Java websites but some I fee are redundant , some are over my head right now etc.  So I'm trying to think of my own practice which will allow me to try and use whatever I am learning at the moment.  I really appreciate the more real life example here.

 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that my example is not very robust. What would happen, for instance, if the URL had no "\\?"?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic