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

How to overcome this problem?  RSS feed

 
Abhishek Reddy
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to split the string based on some special character and display the splitted values accordingly.
My job is to get the values from the text fileds append it with some special charater and store it in database.
While displaying retrieve data split and display accordingly..

let us assume i have to get data from 10 text fileds
now my String format is
String str=data1^^^data4^^^^^^ ;
Now tell me how do i retrieve data which is comming from the 5th/6th/7th textfiled here it is empty.How to test it dynamically and retrieve accordingly?
when i use split i will get array length as 4....so while retrieving every time i have to check for array length and retrive based on it.
ie. if array length is 4 i can retrieve upto 4 values and make others empty.
but what if there are 100 text fields and if 50 are empty....here comes the problem i cant check for each and every time.

this array length varies based on the data entered..
hope you people will understand my problem?
Tell me the better way of doing it..
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, String.split(...) works like you describe - if there are empty fields after the last filled field, they will be omitted from the result array. You could write a simple helper method to make it easier:

[ August 17, 2007: Message edited by: Jesper Young ]
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was gonna say use StringTokenizer, but it also behaves improperly. They must have both been designed by someone who doesn't have a need to parse delimited input lines.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Red Smith:
I was gonna say use StringTokenizer, but it also behaves improperly. They must have both been designed by someone who doesn't have a need to parse delimited input lines.


Another option is to use the overloaded version of the split() method that doesn't delete trailing zero-length matches.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!