Results 1 to 6 of 6
- 04-05-2012, 01:41 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Extract substrings from a string where substring position differs
Str 1 :
^0609|1|2834.56150|07710.32664|17.170|330.60|!
Str 2 :
FC03 00140700 00007D04 0002E702 0001624F 00C02A9D
Str 3 :
^id=PTTRK30ES861001000245502&POS=<3.1><1259.429640 ><07735.382010><084038><260312>@
They are all having the same type of data(U1 L1 L2 T1 T2 S1)
U1|L1|L2|T1|T2|S1|! in Str 1 ( delimiter | )
S1 U1 L1 L2 T2 T1 ins Str 2 (delimiter space)
^id=U1&POS=<T1><L1><L2><S1><T2> ( delimiter >< )
I can have more than these string formats .
I want to parse strings and store them in a list and eventually store
it in a table.
The problem is strings have substrings which appear in different
positions and have different delimiters . I have given examples of some of the strings.
I need a common parser which will work with any string format and also what is the best way to store them.
Thanks
- 04-05-2012, 02:06 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: Extract substrings from a string where substring position differs
Good luck with that.
The idea that you could create something that will work with "any String format" is pie in the sky.Please do not ask for code as refusal often offends.
** This space for rent **
- 04-05-2012, 03:36 PM #3
Re: Extract substrings from a string where substring position differs
If you can't define the rules, you can't parse the String. Simple as that.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 04-09-2012, 07:22 AM #4
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
- 04-09-2012, 02:36 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: Extract substrings from a string where substring position differs
For a string ^0609|1|2834.56150|07710.32664|
I can parse using split .
Is it possible to have a predefined lsit or something which will hold
U1 = 1
L1 = 3
L3 = 2
L4 = 4
and will populate it with the exact values grabbing the substring from position( 1, 3, 2, 4).
U1 = 0609
L1 = 2834.56150
L3 = 1
L4 = 07710.32664
Any ideas on how to do this ?
- 04-09-2012, 10:14 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 6
- Rep Power
- 0
Re: Extract substrings from a string where substring position differs
I'm not sure if I'm answering your question or not, but it sounds like a good use of Maps. I'm still learning, so I'm sure this could be done nicer, but a basic example implementation would be something like:
Java Code:import java.util.*; public class SplitString { public static void main(String[] args) { HashMap<String, String> testMap = new HashMap<String, String>(); String testString = "^0609|1|2834.56150|07710.32664|"; populateMap(testMap, testString.split("[|]")); System.out.println("U1: " + testMap.get("U1")); System.out.println("L1: " + testMap.get("L1")); System.out.println("L3: " + testMap.get("L3")); System.out.println("L4: " + testMap.get("L4")); } private static void populateMap(HashMap<String, String> m, String[] s) { m.put("U1", s[0]); m.put("L3", s[1]); m.put("L1", s[2]); m.put("L4", s[3]); } }
Similar Threads
-
Split string and joining substrings
By KarlNorway in forum New To JavaReplies: 28Last Post: 01-27-2012, 11:51 PM -
Basic string problem, just need to extract two numbers out of string
By gonzoateafly in forum New To JavaReplies: 6Last Post: 12-06-2010, 10:26 AM -
Java help - Replace all if string contains multiple substrings
By bobocheez in forum New To JavaReplies: 9Last Post: 08-31-2010, 06:21 AM -
How to extract substring from a string based on the condition ??
By j_kathiresan in forum Advanced JavaReplies: 5Last Post: 06-22-2010, 10:34 AM -
get position in string from caret position
By helloworld111 in forum AWT / SwingReplies: 5Last Post: 02-19-2009, 02:36 AM
Bookmarks