473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

String.split() not giving proper results

9
HOW to get the spaces out of a delimited string ??
here is a usage scenario where split() seems not to work
Expand|Select|Wrap|Line Numbers
  1. String origAVNListStr = "~`L~`L~`L~`~`~`L~`";
  2. String[] strArr = origAVNListStr.split("~`");
  3.  
  4. for(int i=0;i<strArr.length;i++)
  5. {
  6.        System.out.println(i+1 +"the next token - " + strArr[i]);
  7. }
and the output is
Expand|Select|Wrap|Line Numbers
  1. 1the next token -
  2. 2the next token - L
  3. 3the next token - L
  4. 4the next token - L
  5. 5the next token -
  6. 6the next token -
  7. 7the next token - L
So as we can see - that the last space that shall be displayed as
[HTML]8the next token -[/HTML] is not displayed.

As far as this space reading from the string in an Array is concerned StringTokenizer fails even miserably. It will not take any of the spaces(either at the start/end or in between)

Is there any way out of it. Sincere thanks in advance.
Aug 27 '08 #1
6 1851
blazedaces
284 100+
Trailing spaces are automatically ignored when you use the split method... this is exactly what it's supposed to do.

My question is, why do you need it to write the remaining space? What information does a remaining space give you?

If you know that the number of strings in the array produced by split should be a minimum of, let's say in this case, 8, then why not make that for loop go 8 times and have an if/else statement like so to check if i is < the length of the array. Then just output blanks where you know they would be...

-blazed
Aug 27 '08 #2
giffy
9
Thanks for the prompt reply.

It is a dynamic array situation. The code was just to test what is going wrong.

In addition -
1. I dun know the array size before hand
2. Also the data can be many blanks at the end
3. these are data fields that I need to handle as that only
to be specific
Expand|Select|Wrap|Line Numbers
  1. String theData = "";
Aug 27 '08 #3
blazedaces
284 100+
Thanks for the prompt reply.

It is a dynamic array situation. The code was just to test what is going wrong.

In addition -
1. I dun know the array size before hand
2. Also the data can be many blanks at the end
3. these are data fields that I need to handle as that only
to be specific
Expand|Select|Wrap|Line Numbers
  1. String theData = "";
Question, are you going through many lines of code, each of which may or may not have blanks at the end? Is it that you want them all to be the same size?

I'm still confused as to why seeing the blanks or not is helping you in any way. It can only show you a lack of information.

If you, for example, want all the arrays you obtain from a set of many strings, to be the same size, you could find the length of the longest array in the list and just go through the arrays, recreating a new one with that length and replacing the values along the way AFTER you read through the data once...

Still, what exactly are you trying to do? If I knew that I could help more.

Good luck,

-blazed
Aug 27 '08 #4
JosAH
11,448 Expert 8TB
So as we can see - that the last space that shall be displayed as
[HTML]8the next token -[/HTML] is not displayed.
Note that any string can theoretically be interpreted as ending with infinitely
many empty strings. The split method wisely ignores them all. If you insist
maybe you can use the other (overloaded) two argument split() method. Check
the API documentation for the String class for details.

kind regards,

Jos
Aug 28 '08 #5
giffy
9
Thanks Guys. split() method with two arguments did the trick

here is the code
Expand|Select|Wrap|Line Numbers
  1. String origAVNListStr = "~`L~`L~`L~`~`~`L~`";
  2. String[] strArr = origAVNListStr.split("~`", -1);
  3. for(int i=0;i<strArr.length;i++)
  4. {
  5.      System.out.println(i+1 +"the next token - " + strArr[i]);
  6. }
and the output is as desired with all the sapces being returned and not ignored (at the end)
Expand|Select|Wrap|Line Numbers
  1. 1the next token -
  2. 2the next token - L
  3. 3the next token - L
  4. 4the next token - L
  5. 5the next token -
  6. 6the next token -
  7. 7the next token - L
  8. 8the next token -
Aug 28 '08 #6
itsraghz
127 100+
Well, that's a cool solution suggested by JosAh.

I am also wondering as that blazed, what exactly you would be doing with just the count rather than the valid tokens?
Nov 4 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: oliver | last post by:
hi there i'm experimanting with imaplib and came across stringts like (\HasNoChildren) "." "INBOX.Sent Items" in which the quotes are part of the string. now i try to convert this into a...
6
by: Kristofer Pettijohn | last post by:
I'm a bit unsure about this and the results... but given the following: .... std::string mystring; mystring.reserve(256); strcpy(mystring.c_str(), "Some string C-style"); // C-style copy...
11
by: | last post by:
Hello everyone! :-D OK, I've came across many functions for this, but none works! I need one that works like this: StringSplit(string str, string delim, vector<string> results) Either with...
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
9
by: Java and Swing | last post by:
Say I have a string which contains numbers separated by a comma... such as "0,1,2,3,4,5"...I want to split the string at the commas and return an array containing, 0,1...5. Suggestions? I've...
7
by: Craig C | last post by:
I need to parse through a string for each number and then run a command. Each number only which represents a employee id. string a = "AGENTS\n093333\n8883773\n3338373\nEND OF RESULTS" When I...
2
by: samuelberthelot | last post by:
Hi, I can't figure out how to parse the following string and to decompose it into substring. CuttingFlags--C500F2*#C503F3*#C509F10*#C506F2*F15## Basically, each C should go in an array. and...
12
by: John | last post by:
Hi I have a multi-line address field which has each line separated by CRLF. How can I split this field into individual strings using crlf as separator? Thanks Regards
12
by: Siah | last post by:
Hi, I need to convert the string: '(a, b, "c", d, "e")' into the following list . Much like a csv reader does. I usually use the split function, but this mini-monster wouldn't properly get...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.