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

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


0
anshuma
Asked:
anshuma
3 Solutions
 
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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