Link to home
Start Free TrialLog in
Avatar of Amit
AmitFlag for United States of America

asked on

Need help with a string manipulation

Hi,

I need a java function which takes the following string as input

ALPHA OMEGA(AOMEGA)

and then outputs

ALPHA OMEGA
AOMEGA

So it should split at the braces and return

the two parts of string

The use case for this is a user name coming from windows LDAP and their NTIDs together in a csv file and I need to insert them separately in a database


Avatar of for_yan
for_yan
Flag of United States of America image


String s = "ALPHA OMEGA(AOMEGA)";

String s1 = s.substring(0,s.indexOf("(")) + System.getProperty("line.separator")  +  s.substring(s.indexOf("(")+1,)s.indexOf(")"):
This is testded - use tis code:

        String s1000 = "ALPHA OMEGA(AOMEGA)";

String s1001 = s1000.substring(0,s1000.indexOf("(")) + System.getProperty("line.separator")  +  s1000.substring(s1000.indexOf("(")+1,s1000.indexOf(")"));

        System.out.println(  s1001);

Open in new window


Output:
ALPHA OMEGA
AOMEGA

Open in new window


Another option (don't know if you need one more line to beadded i n foront of the contestnd iof paerenthedss)
   String s1002 = s1000.replaceAll("\\((.*?)\\)"," $1 ");

          System.out.println(  s1002);

Open in new window


Output:

ALPHA OMEGA AOMEGA 

Open in new window

This would be with line break:

  String s1002 = s1000.replaceAll("\\((.*?)\\)","\n$1 ");

          System.out.println(  s1002);

Open in new window


ALPHA OMEGA
AOMEGA 

Open in new window

Avatar of Amit

ASKER

Hi Yan,

Sorry I need it to return two things

so the functions should be returnNTid - AOMEGA
and
functions should be returnFullName - ALPHA OMEGA

can you write this into two functions format
      String s1002 = s1000.replaceAll("\\((.*?)\\)","|$1");

        String [] sss1003 = s1002.split("\\|");

        String s1004 = sss1003[0];
        String s1005  = sss1003[1];

        System.out.println(s1004);
          System.out.println(s1005);

Open in new window



ALPHA OMEGA
AOMEGA

Open in new window

you can use this static method which will return array of two strings:

  public static String [] getTwoStrings (String s1000){
        String s1002 = s1000.replaceAll("\\((.*?)\\)","|$1");

            String [] sss1003 = s1002.split("\\|");

            return sss1003;

    }

Open in new window

Avatar of Amit

ASKER

Oops found some edge cases

NEW USER (1)(NEW USER (1))
NEW USER (2)(NEW USER (2))
NEW USER (3)(NEW USER (3))

the data contains things like these also - How do I handle that
    String s1000 = "ALPHA OMEGA(AOMEGA)";
 for(String s : getTwoStrings(s1000)){
            System.out.println(s);

        }

Open in new window


output:

ALPHA OMEGA
AOMEGA

Open in new window

Avatar of Amit

ASKER

Sorry for my limited (or almost zero) knowledge of java  from the following code How can I selectively
print either ALPHA OMEGA
or
AOMEGA

I tried s[0] and s[1] but it was showing an error

String s1000 = "ALPHA OMEGA(AOMEGA)";
 for(String s : getTwoStrings(s1000)){
            System.out.println(s);

        }
Yuou have to prioveide this method in the same class:

 public static String [] getTwoStrings (String s1000){
        String s1002 = s1000.replaceAll("\\((.*?)\\)","|$1");

            String [] sss1003 = s1002.split("\\|");

            return sss1003;

    }

Open in new window


and quote waht was the error on waht line

That your other case is quite different - look through them all and formulate
how we distingusish different parentheses - explain it to me - I'll
then try to think about something

For now make sure that this one wworks for you
SOLUTION
Avatar of baretree
baretree
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is the complete class which will
split your original string - it compiles and executes as a whole - without any changes.
Try and you'd see it

public class SplitToTwo {


    public static void main(String[] args) {
     String s1000=    "ALPHA OMEGA(AOMEGA)";

            for(String s : getTwoStrings(s1000)){
            System.out.println(s);

        }

    }

      public static String [] getTwoStrings (String s1000){
        String s1002 = s1000.replaceAll("\\((.*?)\\)","|$1");

            String [] sss1003 = s1002.split("\\|");

            return sss1003;

    }
}

Open in new window



ALPHA OMEGA
AOMEGA

Open in new window


About your special case - formulate more strictly how to distinguish the parentheses
and we;ll try to do something about it. It is much more difficult though.
Still full understanding which parentheses not to consider
enclosing parentheses needs clear understanding.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial