• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

split string

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I would like to make an array from the following string in java:

PT3PT6PT7PT8PT9PT10PT11PT13PT14PT15PT16PT17PT19PT20ST6ST7

like

0 = PT3
1 = PT6
2 = PT7
...

What is the simplest way to do that?

Thanks
 
Marshal
Posts: 80740
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to explain exactly what you want from the String. Do you want to divide it into three‑character parts? Do you want to divide when a digit is followed by a letter?
Look at the methods of the Character class, and methods in the String class to get substrings.
 
Bartender
Posts: 5632
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gabriel,

you could program it the hard way: search the string character by character, and noting where
a digit is followed by a capital letter. You know then where an elemant finishes and the next one
starts.

A more OO way would be to use the classes Pattern and Matcher. This will be much easier to
use, but it involves some reading:

http://docs.oracle.com/javase/tutorial/essential/regex/index.html

To make it easier, you might want to get your input in a more easy way to handle, say:

SP1,SP12,...

Then you could simply use


Greetings,
Piet

 
Campbell Ritchie
Marshal
Posts: 80740
485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think the comma solution is an option, I am afraid.
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you want "PT3, PT6...., PT10, PT11,..." I think regex is the best..!!
 
Gabriel Beres
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramesh Pramuditha Rathnayake wrote:if you want "PT3, PT6...., PT10, PT11,..." I think regex is the best..!!



What would be the pattern for that?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gabriel Beres wrote:

Ramesh Pramuditha Rathnayake wrote:if you want "PT3, PT6...., PT10, PT11,..." I think regex is the best..!!



What would be the pattern for that?



That depends on your rules that relate the input to the output, which you still haven't spelled out in detail. Off the top of my head I can immediately see two different possible rule sets that could produce the desired output fro m the provided input.
 
Piet Souris
Bartender
Posts: 5632
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I suggested to change the input string to "PT1,ST12,..." I certainly did not mean to create some
function that would transform the original string into this string. Indeed, if you managed to come
up with such a function, then you had already solved the problem.

What I meant is: that input string is coming from some sort of source. So, go to that source and
try to get that source to deliver that string in a more easy to handle format.

The pattern would be something like "[A-Z][A-Z](\d)+", but read the regex API: it is always a nice
puzzle to construct a correct pattern.

Greetings,
Piet
 
Ranch Hand
Posts: 163
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your data format is always going to be "PT" followed by any number of digits, than a split() would work. You just have to finesse the results.

 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pattern can be given if rules are given.

Campbell Ritchie wrote:You need to explain exactly what you want from the String. Do you want to divide it into three‑character parts? Do you want to divide when a digit is followed by a letter?...



I think you want String end with a number try this...



Don't let us to guess what you want..!
 
Gabriel Beres
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
None of these patterns actually work
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gabriel Beres wrote:None of these patterns actually work



That's not surprising, considering you still haven't told us your exact rules.

Also note that ItDoesntWorkIsUseless(⇐click).
 
Gabriel Beres
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:

Gabriel Beres wrote:None of these patterns actually work



That's not surprising, considering you still haven't told us your exact rules.

Also note that ItDoesntWorkIsUseless(⇐click).




I would like to make an array from the following string in java:

PT3PT6PT7PT8PT9PT10PT11PT13PT14PT15PT16PT17PT19PT20ST6ST7

like

0 = PT3
1 = PT6
2 = PT7
...



That's it. two letters like PT, and a number after
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you better define "a number"?

is -1 possible?

how about 2.839?

how about e or i?

how about 1.189 * 10^14?

all of the above are valid numbers in various situations.

 
Gabriel Beres
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:can you better define "a number"?

is -1 possible?

how about 2.839?

how about e or i?

how about 1.189 * 10^14?

all of the above are valid numbers in various situations.



Any integer like 7 and 3443434
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gabriel Beres wrote:

Jeff Verdegan wrote:

Gabriel Beres wrote:None of these patterns actually work



That's not surprising, considering you still haven't told us your exact rules.

Also note that ItDoesntWorkIsUseless(⇐click).




I would like to make an array from the following string in java:

PT3PT6PT7PT8PT9PT10PT11PT13PT14PT15PT16PT17PT19PT20ST6ST7

like

0 = PT3
1 = PT6
2 = PT7
...



That's it. two letters like PT, and a number after



You and I apparently have different definitions of "exact".

So the grouping for the above would put the PT, the whitespace, and the 3 together as PT3, then the next PT (the one immediately following the 3) goes with the 6, and so on?

Is there always whitespace between the letters and the number? Always one tab? Any number/combination of tabs and spaces? Does the presence or absence of whitespace make a difference?

Are the letters always uppercase?

Are the numbers always integers? Always positive?

You have to specify all that stuff exactly (using my definition of "exact", not yours) if you want to write a regex that captures your rules properly. Computers can't guess at what you mean.
 
Master Rancher
Posts: 5170
83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The patterns given previously probably do work, but not in a split(). That's because in split() you need a regex for the delimiter, not the content between the delimiter. Instead, the regexes given are designed to be used with a Matcher, where you find() each new element. But to do it with split() , you could define a regex that uses lookahead and lookbehind, to find a location in the string that's after a number and before a letter:

 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use Matcher object to find the String, you have to use a while loop. And it will be easier if you use a ArrayList to store the results..

But when you use split() method you can get the result into a String[]..

Other than that you have to know..

Mike Simmons wrote: split() you need a regex for the delimiter, not the content between the delimiter. Instead, the regexes given are designed to be used with a Matcher, where you find() each new element

 
Gabriel Beres
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:The patterns given previously probably do work, but not in a split(). That's because in split() you need a regex for the delimiter, not the content between the delimiter. Instead, the regexes given are designed to be used with a Matcher, where you find() each new element. But to do it with split() , you could define a regex that uses lookahead and lookbehind, to find a location in the string that's after a number and before a letter:



Ohh, good point.
I think i'll just call split("T), and go from that. Letter T is always the secound character.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic