• Post Reply Bookmark Topic Watch Topic
  • New Topic

String split problem with delimiters  RSS feed

 
Chris Kislow
Greenhorn
Posts: 21
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have this code what I want to do is get the string data after the = and then split that data so to show the TCH on one line and the 54455 on a second line
well when i run my code the get TCH54455 on the first line correct, I get the TCH on the second line which is CORRECT but for the last println i get blank, even if I change the index to 0
any help
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are you trying to get from line 16?
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way to see what your code is doing is by instrumenting it with println's.

Also, class names should begin with an upper case letter.
 
Chris Kislow
Greenhorn
Posts: 21
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:What are you trying to get from line 16?


i was hoping that the delimter would split the tch54454 so that I only see the numbers 54454 not the TCH in the beginnning
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are running into a problem with a space character on either side of the equals.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your delimiter of "[^A-Z]" says any character that is NOT an upper case A through Z is a delimiter. This will include the space character.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Kislow wrote:
i was hoping that the delimter would split the tch54454 so that I only see the numbers 54454 not the TCH in the beginnning


If you split the "TCH54454" string, using "[0-9]" as the delimiter, you should get a single value of "TCH".  If you split the "TCH54454" string, using "[^A-Z]" as the delimiter, you should also get a single value of "TCH".

And finally, if you split the "tch54454 string, using "[^A-Z]" as the delimiter, you should get no values.

Henry
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except as written there will be a leading space before TCH.
 
Chris Kislow
Greenhorn
Posts: 21
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Your delimiter of "[^A-Z]" says any character that is NOT an upper case A through Z is a delimiter. This will include the space character.


So which delimiter could I use to remove the space, i did rerun with your suggestion and I saw the digits only buy in the [3] part of the array

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Between your calls to split you can call trim() on the token to remove leading & trailing spaces.

As another way to approach your problem you could instead create a regex with 3 groups. I don't know if you are far enough along in your regex knowledge to handle that.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This should also work

 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With instrumented code I get this for output:


OR with a slightly different delimiter:
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if you have any control over the input but typically name=value pairs don't have spaces around the equals.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!