This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

String split

 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


How to split above String value in to an array with 7 elements. 3rd, 5th and 6th will have null as a value.



Above code generate array with 5 elements only. Last two elements with null value are not getting generated.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it!

 
Marshal
Posts: 67515
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you get two nulls or two empty Strings? An empty string and a null are completely different from each other.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks !

I got the empty String. I just have to verify the resultant array's length, so I am fine with either.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chetan Parekh wrote:Got it!



Can you explain that code a little bit ? What does the -1 do ?
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Laker wrote:

Can you explain that code a little bit ? What does the -1 do ?


It's all there n the API for String#split(...)
The limit parameter controls the number ... If n is non-positive then ...
 
John Laker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Darryl Burke wrote:

John Laker wrote:

Can you explain that code a little bit ? What does the -1 do ?


It's all there n the API for String#split(...)
The limit parameter controls the number ... If n is non-positive then ...



Yes I read it, but had difficulty undestanding this quote there

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.



I'm not sure what pattern they are talking about and how it is applied to what.
 
Ranch Hand
Posts: 57
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Laker wrote:

Yes I read it, but had difficulty undestanding this quote there

The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.



I'm not sure what pattern they are talking about and how it is applied to what.



The 1st argument to split() is a regex pattern.

And if the 2nd parameter is negative (-1), nothing is discarded. So the result is an array with 7 elements with the last 2 are empty stringS. These last 2 elements will be discarded if split("[|]") or split ("[|]", 0) are used
 
John Laker
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

T. Huy Nguyen wrote:

The 1st argument to split() is a regex pattern.

And if the 2nd parameter is negative (-1), nothing is discarded. So the result is an array with 7 elements with the last 2 are empty stringS. These last 2 elements will be discarded if split("[|]") or split ("[|]", 0) are used



I see. The second part makes sense now.

Another question, What is a regex pattern ? I understand that in this example it is "|" which is used to split the string, but what is like a simple definition. Google search did not yield satisfactory results.
 
T. Huy Nguyen
Ranch Hand
Posts: 57
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't know much about regular expression, Wikipedia is a good start. More complete cover of regex and regex support in major programming languages can be found in the book "Mastering regular expression".

But I think a bit of wiki and the javadoc of Pattern class (http://download.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html) are good enough.

 
Saloon Keeper
Posts: 11199
244
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A regex or 'regular expression' is a description of what a String looks like. For example, the pattern "[\\p{Print}&&[^\\\\/:*?\"<>|]]{0,8}" is a description for a string that is between 0 and 8 characters long, and consists of any printable character, except the characters \ / : * ? " < > |

As you can see, regular expressions can become illegible very quickly. To fully understand them you should probably look up a tutorial on regular expressions.
 
I didn't say it. I'm just telling you what this tiny ad said.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!