0

I have a text file in which I have to take line by line.

for each line I have to cut my string and put it in a table.

My separator is space.

Example of String:

22/03/2013 00h00 9940 10200 10260 10190 10030 10060 

Example of result:

[22/03/2013 00h00, 9940, 10200, 10260, 10190, 10030, 10060] 

my problem is that in my file, my separator varies from one line to another

Example:

22/03/2013 00h00 9940 10200 10260 10190 10030 10060 22/03/2013 01h00 9970 9900 9970 9850 9830 9740 22/03/2013 02h00 9630 9750 10010 10100 10040 10010 

How I could cut my string ?

4
  • You can say myString.split(" "); Which will return a String array. You'd have to know what data is in what order though. Some clarification is needed though. Commented Jul 17, 2013 at 13:48
  • @hdtsn my pb is in know how space separate my string Commented Jul 17, 2013 at 13:49
  • 2
    Do you want your first two space separated strings as the same element? Commented Jul 17, 2013 at 13:51
  • 1
    Why do you use a separator that is included in the elements it's meant to separate? Commented Jul 17, 2013 at 13:52

3 Answers 3

2

Don't forget that String.split() takes a regular expression. You can split on multiple whitespaces thus:

myString.split("\\s+"); 

This uses the character class \s, representing any whitespace character (space, tab etc.)

It's not clear how well your data rows are defined in terms of spaces. Your best bet may be to split on whitespace as above, then take the first 2 elements and handle them as a date/time. e.g.

String[] results = myString.split("\\s+"); String datetime = results[0] + " " + results[1]; 

(checking for those result elements existing, etc.)

Sign up to request clarification or add additional context in comments.

1 Comment

ok, but i want to put this line in my table :22/03/2013 00h00 and there is a space ??
1

You can try this regex

str.split("(?<!/\\d{4}) +"); 

1 Comment

ok, but i want to put this line in my table :22/03/2013 00h00 and there is a space ??
0

Something like should work for you:

String str = "22/03/2013 00h00 9940 10200 10260 10190 10030 10060 " + "22/03/2013 01h00 9970 9900 9970 9850 9830 9740 " + "22/03/2013 02h00 9630 9750 10010 10100 10040 10010"; // split string into individual records String[] rows = str.split("\\s+(?=\\d{2}/\\d{2}/\\d{4} )"); // now split each row into columns (combine first 2 columns into 1) for (String r: rows) System.out.println(Arrays.toString(r.split("(?<!\\d{2}/\\d{2}/\\d{4})\\s+"))); 

OUTPUT:

[22/03/2013 00h00, 9940, 10200, 10260, 10190, 10030, 10060] [22/03/2013 01h00, 9970, 9900, 9970, 9850, 9830, 9740] [22/03/2013 02h00, 9630, 9750, 10010, 10100, 10040, 10010] 

Explanation: First regex to split by 1 or more space/newline characters if it is followed by a date in nn/nn/nnnn format \\d{2}/\\d{2}/\\d{4}.

Second regex is to split columns by 1 or more space/newline characters if it is NOT preceded by a date in the same format thus ignoring very first space after date.

1 Comment

I think some explanation would be useful here, given the regexp functions being used

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.