String Manipulation: Separating Separated Data

I need to break down some information from the data marked with an asterisk.

Data format:

NAME*ADRESS LINE1*ADDRESS LINE2 

Rules:

 1. Name should be always present 2. Address Line 1 and 2 might not be 3. There should be always three asterisks. 

Examples:

 MR JONES A ORTEGA*ADDRESS 1*ADDRESS2* Name: MR JONES A ORTEGA Address Line1: ADDRESS 1 Address Line2: ADDRESS 2 A PAUL*ADDR1** Name: A PAUL Address Line1: ADDR1 Address Line2: Not Given 

My algorithm:

 1. Iterate through the characters in the line 2. Store all chars in a temp variables until first * is found. Reject the data if no char is found before first occurence of asterisk. If some chars found, use it as the name. 3. Same as step 2 for finding address line 1 and 2 except that this won't reject the data if no char is found 

My algo looks ugly. The code looks more ugly. Split using // * does not work, since the name can be replaced with address line 1, if the data was * Address 1 * Address2. Any suggestion?

EDIT:

Try using the data, excluding the quotation marks "-MS DEBBIE GREEN * 1036 PINEWOOD CRES **"

+4
source share
5 answers

You can use String[] split(String regex, int limit) as follows:

  String[] tests = { "NAME*ADRESS LINE1*ADDRESS LINE2*", "NAME*ADRESS LINE1**", "NAME**ADDRESS LINE2*", "NAME***", "*ADDRESS LINE1*ADDRESS LINE2*", "*ADDRESS LINE1**", "**ADDRESS LINE2*", "***", "-MS DEBBIE GREEN*1036 PINEWOOD CRES**", }; for (String test : tests) { test = test.substring(0, test.length() - 1); String[] parts = test.split("\\*", 3); System.out.printf( "%s%n Name: %s%n Address Line1: %s%n Address Line2: %s%n%n", test, parts[0], parts[1], parts[2] ); } 

This prints ( as seen on ideone.com ):

 NAME*ADRESS LINE1*ADDRESS LINE2* Name: NAME Address Line1: ADRESS LINE1 Address Line2: ADDRESS LINE2 NAME*ADRESS LINE1** Name: NAME Address Line1: ADRESS LINE1 Address Line2: NAME**ADDRESS LINE2* Name: NAME Address Line1: Address Line2: ADDRESS LINE2 NAME*** Name: NAME Address Line1: Address Line2: *ADDRESS LINE1*ADDRESS LINE2* Name: Address Line1: ADDRESS LINE1 Address Line2: ADDRESS LINE2 *ADDRESS LINE1** Name: Address Line1: ADDRESS LINE1 Address Line2: **ADDRESS LINE2* Name: Address Line1: Address Line2: ADDRESS LINE2 *** Name: Address Line1: Address Line2: -MS DEBBIE GREEN*1036 PINEWOOD CRES** Name: -MS DEBBIE GREEN Address Line1: 1036 PINEWOOD CRES Address Line2: 

The reason for "\\*" is that split accepts a regular expression, and * is a metacharacter for regular expressions, and since you want it to mean literally, it needs to be escaped with \ . Since \ itself is the escape character of a Java string, to get \ in a string, you need to double it.

The reason for limit in 3 is because you want the array to have 3 parts, including the completion of empty lines. A limit -less split by default discards empty lines.

The last * discarded manually before split executed.

+2
source
 String myLine = "name*addr1*addr2*" String[] parts = myLine.split('\\*',4); for (String s : parts) { System.out.println(s); } 

Conclusion:

 name addr1 addr2 (empty string) 

If you divide by "**addr2*" , you will get an array with "," "," addr2 ". Therefore, I do not understand why you cannot use split.

In addition, if you divide "***" , you will get an array of 4 elements with 4 empty lines.

Here you get an example, try running this code:

 public void testStrings() { String line = "part0***part3*part4****part8*"; String[] parts = line.split("\\*"); for (int i=0;i<parts.length;i++) { System.out.println(String.format("parts[%d]: '%s'",i, parts[i])); } } 

The result will be:

 parts[0]: 'part0' parts[1]: '' parts[2]: '' parts[3]: 'part3' parts[4]: 'part4' parts[5]: '' parts[6]: '' parts[7]: '' parts[8]: 'part8' 
0
source

You can use regex for this. For instance:

 String myInput="MR JONES A ORTEGA*ADDRESS 1*ADDRESS2*"; Pattern pattern = Pattern.compile("([^*]+)\\*([^*]*)\\*([^*]*)\\*"); Matcher matcher = pattern.matcher(myInput); if (matcher.matches()) { String myName = matcher.group(1); String myAddress1 = matcher.group(2); String myAddress2 = matcher.group(3); // ... } else { // input does not match the pre-requisites } 
0
source

Complete solution, reading from a file using a scanner and regular expressions:

 import java.io.*; import java.util.Scanner; import java.util.regex.Pattern; public class Test { public static void main(String[] args) throws FileNotFoundException { Scanner s = new Scanner(new File("data.txt")); Pattern p = Pattern.compile("([^\\*]+)\\*([^\\*]*)\\*([^\\*]*)\\*"); while (s.hasNextLine()) { if (s.findInLine(p) == null) { s.nextLine(); continue; } System.out.println("Name: " + s.match().group(1)); System.out.println("Addr1: " + s.match().group(2)); System.out.println("Addr2: " + s.match().group(3)); System.out.println(); } } } 

Input file:

 MR JONES A ORTEGA*ADDRESS 1*ADDRESS2* A PAUL*ADDR1** *No name*Addr 2* My Name*Some Addr*Some more addr* 

Conclusion:

 Name: MR JONES A ORTEGA Addr1: ADDRESS 1 Addr2: ADDRESS2 Name: A PAUL Addr1: ADDR1 Addr2: Name: My Name Addr1: Some Addr Addr2: Some more addr 

Note that a string without a name is not matched (as per Rule 1: Name should be always present ). If you still want to process these lines, just change the + in the regular expressions to * .

Regular expressions ([^\\*]*)\\* can be read as: "Everything except an asterisk, zero or more, and then an asterisk."

0
source

yourString.split("\\*"); should provide you with an array with name, address1 and address2, where, since address1 and address2 can be empty. Additional information: here

-1
source

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


All Articles