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


anshumaEngineeringAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:

String s = "ALPHA OMEGA(AOMEGA)";

String s1 = s.substring(0,s.indexOf("(")) + System.getProperty("line.separator")  +  s.substring(s.indexOf("(")+1,)s.indexOf(")"):
0
for_yanCommented:
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

0
for_yanCommented:

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

0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

for_yanCommented:
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

0
anshumaEngineeringAuthor Commented:
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
0
for_yanCommented:
      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

0
for_yanCommented:
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

0
anshumaEngineeringAuthor Commented:
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
0
for_yanCommented:
    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

0
anshumaEngineeringAuthor Commented:
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);

        }
0
for_yanCommented:
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
0
baretreeCommented:
you can access individual elements with the [position] that is 0 based
the problem might be that with that function you're not receiving two elements in the array, but just one element that is separated with newlines(in case you didn't change the "\n$1 " part for the "\$1" he latter provided to you.

if you call that function and then do a System.out.println(s1000[0]); then it should work
in example:

    public static void main(String[] args) {
        String[] resultString = getTwoStrings("ALPHA OMEGA (AOMEGA)");
        System.out.println(Arrays.toString(resultString));
        System.out.println(resultString[1]);
        System.out.println(resultString[0]);
    }

Open in new window


the output will show you

[ALPHA OMEGA , AOMEGA]
AOMEGA
ALPHA OMEGA
0
for_yanCommented:
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.
0
for_yanCommented:
This class and method getTwoStrings2 will work for both cases

 "ALPHA OMEGA(AOMEGA)"  and "NEW USER (1)(NEW USER (1))"

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SplitToTwo {


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


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

        System.out.println("");
                s1000=  "NEW USER (1)(NEW USER (1))";

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

        }

    }

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

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

            return sss1003;

    }

        public static String [] getTwoStrings2 (String s1000){
            String [] ss = new String[2];

            Pattern p1100 = Pattern.compile("\\(([^\\(\\)]*\\([^\\(\\)]*\\)[^\\(\\)]*)\\)");
                                                 Matcher m1100 = p1100.matcher(s1000);

                                         while(m1100.find()){

                                            // System.out.println("one");
                                                            ss[0] = s1000.substring(0, m1100.start());
                                                            ss[1] = m1100.group(1);

                                                 return ss;


                                         }
             Pattern p1101 = Pattern.compile("\\(([^\\(\\)]*)\\)");
                                  Matcher m1101 = p1101.matcher(s1000);
               while(m1101.find()){
                                          //  System.out.println("two");
                                                            ss[0] = s1000.substring(0, m1101.start());
                                                            ss[1] = m1101.group(1);
                                                 return ss;


                                         }



                             return ss;



        }


}

Open in new window


ALPHA OMEGA
AOMEGA

NEW USER (1)
NEW USER (1)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
>>
Sorry for my limited (or almost zero) knowledge of java  from the following code How can I selectively
print either ALPHA OMEGA
or
AOMEGA
>>

Select either atoms[0] or atoms[1] in the below:
String s = "ALPHA OMEGA(AOMEGA)";
	String[] atoms = s.split("[()]");

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.