Amit
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
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
This is testded - use tis code:
Output:
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);
Output:
ALPHA OMEGA
AOMEGA
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);
Output:
ALPHA OMEGA AOMEGA
This would be with line break:
String s1002 = s1000.replaceAll("\\((.*?)\\)","\n$1 ");
System.out.println( s1002);
ALPHA OMEGA
AOMEGA
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
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);
ALPHA OMEGA
AOMEGA
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;
}
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
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);
}
output:
ALPHA OMEGA
AOMEGA
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);
}
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:
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
public static String [] getTwoStrings (String s1000){
String s1002 = s1000.replaceAll("\\((.*?)\\)","|$1");
String [] sss1003 = s1002.split("\\|");
return sss1003;
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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.
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;
}
}
ALPHA OMEGA
AOMEGA
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
String s = "ALPHA OMEGA(AOMEGA)";
String s1 = s.substring(0,s.indexOf("(