Create a new line with several random new lines

I have a line:

String text = "Nothing right in my brain. Nothing left in my brain" 

I want to create a new line text2 that has 2-4 random new lines from the previous ones, for example:

 "Nothing \n right \n in my brain. \n Nothing left in my brain" 

or

 "Nothing right in \n my brain. Nothing left in \n my brain" 

How to create a new line with a random new line between words? I want to get a space index to insert a new line after a random space. But I only get the first space index. Does anyone know a better approach to solve this problem? Thanks.

+5
source share
6 answers

There are three steps to your problem: splitting String , inserting randomness and using them together ...

String separation

Divide it into words using String.split () , which creates an array of strings (in this case words) with spaces.

  String[] words = text.split(" "); //Split by spaces 

then rebuild String with new lines added: -

  StringBuiler sb = new StringBuilder(); for (String word : words) { sb.append(word + "\n"); } String text2 = sb.toString(); 

In this case, you insert a new line between each word and save the result in text2 .

Randomness insert

You can just create a Random object ...

  Random random = new Random(); 

Then use it in code that inserts your new line, for example ...

 //Randomly add or don't add a newline (Compacted with a ternary operator instead of an 'if') sb.append(word + (random.nextBoolean() ? "\n" : "")); 

Association (TL; DR)

Then you only need to save the number of inserted rows and limit them. So your code will be: -

  int requiredNewlines = random.nextInt(2 - 5) + 2; //Number between 2-4 for (String word : words) //For each word { sb.append(word); //Add it if (requiredNewlines >= 0 && random.nextBoolean()) { //Randomly add newline if we haven't used too many sb.append("\n"); requiredNewlines--; } } String text2 = sbAppen.toString(); 

Additionally

Although the randomness in this example matches your purpose here, it is not an ideal implementation of random ones (as mentioned in the comments), as there is a more biased attitude towards what is approaching the beginning of the String and that there is no chance that it will appear before the first word.

There is another option for using StringTokenizer instead of String.split () , which I prefer but does not deal with regex and does not work, so I changed my answer to using String.split ()

+5
source

First you need a new random from 2-4:

 int max = 4; int min = 2; Random rand = new Random(); int randomNum = rand.nextInt((max - min) + 1) + min; 

After dividing the string into words:

 String[] words = text.split(" "); 

Then we get 4 different numbers from 1 to words.length

 ArrayList<Integer> randomPositions = new ArrayList<Integer>(randomNum); max = words.length; min = 1; for (int count = 0; count < randomNum; count ++) { int random = rand.nextInt((max - min) + 1) + min; if (randomPositions.contains(random)) count --; else randomPositions.add(random); } 

Finally, put \n in position when rebuilding the array:

 StringBuilder result = new StringBuilder(); for (int count = 0; count < max; count ++) { result.append(words[count]); if (randomPositions.contains(count)) result.append("\n"); else result.append(" "); } 

Check out this working demo.

Output1:

 Nothing right in my brain. Nothing left in my brain 

Output2:

 Nothing right in my brain. Nothing left in my brain 

output3:

 Nothing right in my brain. Nothing left in my brain 
+1
source

You can only get the first index using indexOf() or the last index using lastIndexOf() methods.

But as a workaround, you can use text.indexOf(" ", randomInt); . This will give you the first index "" after the randomInt index.

0
source

First you can get a random index where you can put a new row

 Random rand = new Random(); String[] words = text.split(' '); for(int i = 0; i < 3; i++){ int index = rand.nextInt(words.length()); words[index] = "\n" + words[index]; } text = Arrays.toString(words); 

and in the end you will have 3 new lines in random places in the line text

EDIT: one way to get the result without commas is as follows:

 text = Arrays.toString(words).replaceAll(", ", " "); 
0
source
 String str = "Nothing right in my brain. Nothing left in my brain"; String[] split = str.split(" "); for (int i = 0; i < split.length; i++) { int flag = ((int) (Math.random() * 10)) % 2; if (flag == 0) { split[i] = split[i] + "\n"; } } str=""; for (int i = 0; i < split.length; i++) { str += split[i]+" "; } 
0
source

Create a row with inserts in different positions:

 public static String generateString(String inputString, String delimiter, String insString, int[] insIndexes) { String[] splittedString = inputString.split(delimiter); StringBuilder sb = new StringBuilder(); for (int i = 0; i < splittedString.length; i++) { sb.append(splittedString[i]); if (Arrays.binarySearch(insIndexes, i + 1) >= 0) sb.append(delimiter + insString); sb.append(delimiter); } return sb.toString(); } 

Part of the call:

  String inputString = "Nothing right in my brain. Nothing left in my brain"; String delimiter = " "; // input String delimiter String insString = "\n"; // insertion String int[] insIndexes = { 2, 4, 6, 8 }; // insertion indexes String outputString = generateString(inputString, delimiter, insString, insIndexes); 
0
source

All Articles