• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

String manipulation

I have strings ex: <null>, <test|test1|test3|test4|null>. Between the angle brackets if I see pipe delimited values, I need to get each value. If I see null without pipes, I need to process database updaate. Could anyone provide a sample code????
0
newuser11
Asked:
newuser11
  • 5
  • 3
2 Solutions
 
petmagdyCommented:
try this:

    public static void main(String[] args) {
        String theInput = "<null>, <test|test1|test3|test4|null>";
        String[] tokens = theInput.split("[,<>|]");
        for(int i=0; i < tokens.length; i++)
        {
            System.out.println(tokens[i]);
        }
    }
0
 
petmagdyCommented:
or better:

public class SampleParse {
   
    public static void doSingleDdOperation(String row)
    {
        String[] values = row.split("[,><|]");
                for(int i=0; i < values.length; i++)
                {
                    System.out.println(values[i]);

                }
       
    }
   
    public static void main(String[] args) {
        String theInput = "<null>, <test|test1|test3|test4|null>";
        String[] rows = theInput.split("null>");
        for(int i=0; i < rows.length; i++)
        {
            System.out.println(rows[i]);
            doSingleDdOperation(rows[i]);
        }
    }
}
0
 
newuser11Author Commented:
Thank you so much for your help. Can you also help me with the following regular expression.

String theInput = "<test,tester|test1|test3,tester|test4|null, one,two|three|four>";

If I have a "," in between the "test,tester", I want to return this as one name. For the split regular expression,  how to say return only

test,tester|test1|test3,tester|test4|null

one,two|three|four

as two tokens.

Thanks.


0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
petmagdyCommented:
then try this:

      public static void main(String[] args) {
            String theInput = "<test,tester|test1|test3,tester|test4|null, one,two|three|four>";
            String[] rows = theInput.split("(<)|(>)|(, )");
            for(int i=0; i < rows.length; i++)
            {
                  System.out.println(rows[i]);
//                  doSingleDdOperation(rows[i]);
            }
      }
0
 
newuser11Author Commented:
Thanks one more time.

The actual string I am trying with is

<[10307|sdme|20011002        |xxxx, xxxxx||||||AF|RURAL||||20010924|20880808, 10308|sdme|20011002        |YYY YYYY, TEST|DOB: 30 JUL 57; POB: Care, Naga land|||||XX|ALL||||20010924|20880808,null]>
From this string I want

10307|sdme|20011002        |xxxx, xxxxx||||||AF|RURAL||||20010924|20880808
10308|sdme|20011002        |YYY YYYY, TEST|DOB: 30 JUL 57; POB: Care, Naga land|||||XX|ALL||||20010924|20880808

as two tokens.  split("(<)|(>)|(, )") is giving four tokens.

10307|sdme|20011002        |xxxx
xxxxx||||||AF|RURAL||||20010924|20880808
10308|sdme|20011002        |YYY YYYY
TEST|DOB: 30 JUL 57; POB: Care, Naga land|||||XX|ALL||||20010924|20880808

Can you help again with this.

Thanks.
 






0
 
petmagdyCommented:
then ur regex will look like:

"(<[)|(]>)|(, )|(,null)"
0
 
newuser11Author Commented:
No, it's still giving me four tokens.  In this regex, how to write apply comma rule only after 16 pipe.

Thanks.

0
 
RMaruszewskiCommented:
You shouldn't do it.
You should have more general method to parse such strings.

If you want to parse this line:
<[10307|sdme|20011002        |xxxx, xxxxx||||||AF|RURAL||||20010924|20880808, 10308|sdme|20011002        |YYY YYYY, TEST|DOB: 30 JUL 57; POB: Care, Naga land|||||XX|ALL||||20010924|20880808,null]>

Use this algorithm:

1. Cut off "<[" and "]>" from the beginning and the end of the string:
  input = input.substring(2, input.length() - 2);

2. Split the string on '|' character:
  String[] fields = input.split("\|");

3. Get fields number 1 and 16:
  String field1 = fields[0]; // should be "10307"
  String field16 = fields[15]; // should be "20880808, 10308"

4. Split field16 on ',' character:
  String[] parts = field16.split(",");

5. Get both parts from field16, and trim them if necessary:
  String part1 = parts[0].trim(); // should be "20880808"
  String part2 = parts[1].trim(); // should be "10308"
0
 
petmagdyCommented:
sorry the code is not very optimized but I have no more time to think it over but works however:

import java.util.Vector;

public class RegexTest {
       
    public static String[] separateAfter16Pipe(String singleRow)
    {
        String[] elements  = singleRow.split("\\Q|\\E");
        String[] results = null;
        System.out.println(elements.length);
        if(elements.length < 16)
        {
            results = new String[1];
            results[0]=singleRow;
            return results;
        }
        Vector resultVector = new Vector();
        int j=0;
        String concat = "";
        for(int i=0; i < elements.length; i++)
        {
            if(j==15)
            {
                String[] splited = elements[i].split(", ");
                concat += splited[0];
                resultVector.add(concat);
                concat = "";
                if(splited.length >= 2)
                {
                    concat += splited[1];
                    if(i < (elements.length-1) )
                    {
                        concat += "|";
                    }
                }
                j=0;
            }
            else if(i == (elements.length-1))
            {
                concat += elements[i];
                resultVector.add(concat);            
            }
            else
            {
                concat += elements[i];
                if(i < (elements.length-1) )
                {
                    concat += "|";
                }
                j++;
            }
        }
        results = (String[]) resultVector.toArray(new String[0]);
        return results;
    }
   
    public static void printArrayElements(String[] arr)
    {
        for(int i=0; i < arr.length; i++)
        {
            System.out.println(arr[i]);
        }        
    }
   
    public static void main(String[] args) {
        String theInput = "<[10307|sdme|20011002        |xxxx, xxxxx||||||AF|RURAL||||20010924|20880808, 10308|sdme|20011002        |YYY YYYY, TEST|DOB: 30 JUL 57; POB: Care, Naga land|||||XX|ALL||||20010924|20880808,null]>";
        String[] rows = theInput.split("(\\Q<[\\E)|(\\Q]>\\E)|(,null)");
        for(int i=0; i < rows.length; i++)
        {
            System.out.println(rows[i]);
            String[] result = separateAfter16Pipe( rows[i]);
            printArrayElements(result);
        }
    }
}
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now