0

I have to process many strings( thousands ) which are in either of the two formats as below:

============= Examples of Str=============
123|S|122.14, 
S,344,122.146 

==============================================
The format of these strings are 
A|B|C 
B,A,C 

I want them as A=123 B=S C=122.14

and A=344, B=S, C=122.146 ( where A , B , C correspond to column names and can be loaded into sql server db )

How do I do this.

I can use split to get the substrings as values. How do I map these values with formats and load them?

Please help. Thanks

2 Answers 2

1

You could do something like:

String str = "123|S|122.14,";
String[] tokens = str.split("[|,]");
String A;
String B;
String C;
if(tokens[0].equalsIgnoreCase("S"))
{
    A = tokens[1];
    B = tokens[0];
    C = tokens[2];
}
else            
{
    A = tokens[0];
    B = tokens[1];
    C = tokens[2];
}
Sign up to request clarification or add additional context in comments.

1 Comment

@AlexeiKaigorodov: You are right. I missed that. Updated the answer.
0
String[] ABC; 
if( line.contains( '|' ) ) {
   ABC = str.split( "|" ); 
} else {
   String[] split = str.split( "," ); 
   ABC = new Object[] { split[1], split[0], split[2] ); 
}

1 Comment

And then String A = ABC[0], B=ABC[1], C=ABC[2] if you prefer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.