0

I have a very long string with prices of about 400 000 and even more..

String s_prices = "19; 16; 20; 01; 16; 1.3; 1.6; 50; 2.0; 17; ..."

Then from this very long string, I want to cut it into multiple substrings , and each sub-string should have 3 prices, example:

19, 16, 20 

01, 16, 1.3

1.6, 50, 2.0 

and more...

How do I create sub-strings each with three prices?

Exsent
  • 25
  • 6
  • Welcome to Stack Overflow! In general on this site, questions should be limited to a single question, and I see three distinct questions in here: how to split into substrings containing three numeric values, how to get the highest and lowest value from a string containing multiple numeric values, and how to get the first and last value from a string containing multiple numeric values. You would be better off asking each question individually. – M. Justin Apr 03 '22 at 04:09
  • 1
    You might want to break this down into a number of steps. (1) Split the text up wherever there's a semicolon, (2) convert each piece into a suitable data type, (3) organise the numbers into groups of three, (4) find the highest and lowest within each group. Concentrate on solving one step at a time, before moving onto the next. – Dawood ibn Kareem Apr 03 '22 at 04:12
  • Is it intentional that your example substrings changed the delimiter from semicolon to comma? – M. Justin Apr 03 '22 at 04:13
  • yes its intentional, I want it that way, or anyhow, as long um getting the same end results – Exsent Apr 03 '22 at 04:20
  • To follow up on @DawoodibnKareem, here's some pointers to the different parts: (1) https://stackoverflow.com/q/10631715/1108305, (2) https://stackoverflow.com/q/23057549/1108305, (3) https://stackoverflow.com/q/12026885/1108305, (4) https://stackoverflow.com/q/22335544/1108305 – M. Justin Apr 03 '22 at 04:21
  • 1
    It is now one question, those that had suggested – Exsent Apr 03 '22 at 04:23

1 Answers1

1

This can be accomplished by splitting the string into a list containing the individual elements, partitioning that into groups of three, and then joining the elements in each partition back together.

String s_prices = "19; 16; 20; 01; 16; 1.3; 1.6; 50; 2.0; 17";

// Convert to List of strings:
// ["19", "16", "20", "01", "16", "1.3", "1.6", "50", "2.0", "17"]
List<String> prices = Arrays.asList(s_prices.split("; "));

// Convert to List of 3-element lists of strings (possibly fewer for last one):
// [["19", "16", "20"], ["01", "16", "1.3"], ["1.6", "50", "2.0"], ["17"]]
List<List<String>> partitions = new ArrayList<>();
for (int i = 0; i < prices.size(); i += 3) {
    partitions.add(prices.subList(i, Math.min(i + 3, prices.size())));
}

// Convert each partition List to a comma-delimited string
// ["13, 16, 20", "01, 16, 1.3", "1.6, 50, 2.0", "17"]
List<String> substrings =
        partitions.stream().map(p -> String.join(", ", p)).toList();

// Output each list element on a new line to view the results
System.out.println(String.join("\n", substrings));

Output:

19, 16, 20
01, 16, 1.3
1.6, 50, 2.0
17
M. Justin
  • 14,487
  • 7
  • 91
  • 130
  • This is exactly what I want, however I did a small adjustment to this: List substrings = partitions.stream().map(p -> String.join(", ", p)).collect(Collectors.toList()); – Exsent Apr 03 '22 at 05:03
  • May you answer my second question here: https://stackoverflow.com/questions/71723416/how-to-find-highest-and-lowest-value-on-substrings – Exsent Apr 03 '22 at 06:07