Separation of a line with separators with a period of several lines

I have a line

String x = "Hello.August 27th.Links.page 1"; 

I am wondering if I can split this line into 4 other lines based on where the period is. For example, four other lines will be,

 String a = "Hello"; String b = "August 27th"; String c = "Links"; String d = "page 1"; 

As you can see, I basically want to extract some parts of the line from a new line, the place where it is extracted is based on where the period ends, which ends the first line, and then shows where the second one is, etc. .. end lines.

Thanks in advance!

In android btw

+4
source share
4 answers

Use String#split (note that it gets the regex as a parameter)

 String x = "Hello.August 27th.Links.page 1"; String[] splitted = x.split("\\."); 
+17
source

Yes, of course, just use:

  String[] stringParts = myString.split("\\.") 
+2
source
 String x = "Hello.August 27th.Links.page 1" String []ar=x.split("[.]"); 
+2
source

Perhaps you can use StringTokenizer for this requirement. Here is a simple approach:

 String x = "Hello.August 27th.Links.page 1"; if (x.contains(".")) { StringTokenizer stringTokenizer = new StringTokenizer(x, "."); String[] arrayOfString = new String[stringTokenizer.countTokens()]; int i = 0; while (stringTokenizer.hasMoreTokens()) { arrayOfString[i] = stringTokenizer.nextToken(); i++; } System.out.println(arrayOfString[0]); System.out.println(arrayOfString[1]); System.out.println(arrayOfString[2]); System.out.println(arrayOfString[3]); } 

You are done. :)

0
source

Source: https://habr.com/ru/post/1369259/


All Articles