user180175
user180175

Reputation: 23

mapping string values with corresponding formats

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

Upvotes: 0

Views: 89

Answers (2)

dpnmn
dpnmn

Reputation: 543

String[] ABC; 
if( line.contains( '|' ) ) {
   ABC = str.split( "|" ); 
} else {
   String[] split = str.split( "," ); 
   ABC = new Object[] { split[1], split[0], split[2] ); 
}

Upvotes: 0

juergen d
juergen d

Reputation: 204904

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];
}

Upvotes: 1

Related Questions