0

have the source string

str = "one
 two
 three
 four
 five
 six
 seven
 eight
 nine" 

I want to get an array of strings

String[] mas; 
mas[0] = "one
 two
 three
 four" 

mas[1] =five 
six 
seven 
eight" 

mas[2] ="nine"

I want a function that will be out of my line to do an array of strings. And in each row of the array should get 4 or 6 or N lines from the original string

4 Answers 4

5

Use String.split() with a regex \\s+ (or \n+ for example, depending on what you exactly want).

You can concat 2 or more strings if needed, but after you have the array using split() - it should be fairly easy, give it a try!

good luck. Feel free to ask any question if you are having troubles implementing the 2nd part.


EDIT: seems like you are having troubles with concatting the strings. This shou;d be done with simple iteration and a StringBuilder:

    int SIZE = 4; 
    String[] arr = str.split("\\s+");
    List<String> res = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < arr.length; i++) {
        sb.append(arr[i]).append('\n');
        if (i % SIZE == SIZE-1) { 
            res.add(sb.toString());
            sb  = new StringBuilder();
        }
    }
    if (sb.length() != 0) res.add(sb.toString());
    System.out.println(res);
}

Note that res is a List<String>, if you want it as an array, you can always use the toArray() method.

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

4 Comments

In fact of the matter is that I get an array of strings. But I can not then create another array in which each element will contain 4 lines
@user2954895 Why is that? What's your problem is? Are you familiar with concatting strings or using a StringBuilder?
yes I know. the problem is that I have an array of strings. example of 1000 elements. I do not understand the algorithm, how do I make another array, each element of which is equal to 1-20 rows of the first array. the second element of the first array of rows 20-40. etc.
@user2954895 I editted the answered and refered your question. Hope that is clear.
1

If you want to split into an array with one line per array element, then String.split should do this; see @amit's answer.

If you want to split into an array with N lines per array element (as per your example), there is no standard library class / method to do this. And as far as I'm aware there is no method in any of the commonly used 3rd-party libraries to do this.

So what you could do is one of the following:

  • Split into an array of single lines ... and glue the arrays back together in groups of N.
  • Repeatedly call Matcher.find(...) to match / step through the line breaks, and every N times, you call String.substring(...) to pull out a group of lines.

(It might also be possible to produce a gnarly regex with a complicated positive look-behind that served to effectively count N line breaks. However, that is (IMO) "nasty" ...)

Comments

1
@Test
public void test(){
    String str = "one\ntwo\nthree\nfour\nfive\nsix\nseven\neight\nnine";
    int number = 52;
    String[] strArr = getArray(str, 52);
}

String[] getArray(String str, int number){
    int nb = (int) Math.ceil(number / 20);
    String[] strSplit = str.split("\n");
    String[] strArr = new String[nb+1];
    int cursor = 0;
    for (int colNumberForFinalArray=0; colNumberForFinalArray<nb+1; colNumberForFinalArray++){
        strArr[colNumberForFinalArray] = "";
        int countElts = 20;
        if(colNumberForFinalArray == nb){
            countElts = number % 20;
        }
        for(int i = cursor; i< cursor+countElts ; i++){
            strArr[colNumberForFinalArray] += strSplit[i];
            if(i < cursor+countElts-1){
                strArr[colNumberForFinalArray] += "\n";
            }
        }
        cursor = cursor+countElts;
    }
    return strArr;
}

2 Comments

Yes, you understood what I want))) only a few friends) I have a number of rows. anytime. I do not know him. but I know exactly what I need to break 20. for example if the result is 51 20 20 11 if 52 then 20 20 12 how can I fix function to not ask for the last digit?
I didn't test because the str in input here is to small, but it would be something like this... Don't forget to manage an exception if the str in input is to small.. And if it works accept the answer plz ;)
0

Call this method and you will get an Array of your string

public String[] stringSplitter(String input){
     String[] splitStr = input.split("\\s+");
     return splitStr ;
 }

Call the following method to organize your result in groups of four

 public ArrayList<String> stringGrouping(String[] input){
     int iter = 4;
     ArrayList<String> result = new ArrayList<String>();
     for(int i=0; i<input.length(); i++){
          String temp="";
          while(iter>0){
              temp.append(input.get(i));
          }
          result.add(temp);
          iter =4;
     }

Description:

-The method split take a regex input to detect the char that you want to divide with and return an Array of splitted words.

-The regex "\s+" detects any occurrence of One or more spaces.

I hope it helps

3 Comments

what it means "\\s+" ??? Your function returns me an array of strings, each of which is only one line of the original string. I need one line to the resulting array contained the 4 lines from the original string
@user2954895 check the modified answer. I hope I have understand you well.
unfortunately this is not what I need ((( I say what I do. I have a text. large. I save it in a string. Then I want the text on the page break. One page contains 20 lines. I can beat all of the text on one line. But I can not from the resulting machchiva, form page 20 rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.