Click here to Skip to main content
15,561,817 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a String that I want to Split on basis of diffrent word.

string s = "MSH|^~\&|EPIC|EPICADT|SMS|SMSADT|199912271408|CHARRIS|ADT^A04|1817457|D|2.5|PID||0493575^^^2^ID 1|454721||DOE^JOHN^^^^|DOE^JOHN^^^^|19480203|M||B|254 MYSTREET AVE^^MYTOWN^OH^44123^USA||(216)123-4567|||M|NON|400003403~1129086|NK1||ROE^MARIE^^^^|SPO||(216)123-567||EC|||||||||||||||||||||||||||
PV1||O|168 ~219~C~PMA^^^^^^^^^||||277^ALLEN MYLASTNAME^BONNIE^^^^||||||||||||2688684|||||||||||||||||||||||||199912271408||||||002376853 "

Here, I want to split this string into multiple line string wherever we found following words in

(MSH,PID, NK1, PV1 )

pleasse give me a solution if anyone have it.
Updated 21-Oct-13 7:50am

If you are actually handling real HL7 messages, you should study the HL7 specification a little closer. I have found the links below to be quite useful:[^][^]

Make sure you check out this[^] small sample, which says (among other things) "A carriage return character (\r, which is 0D in hexadecimal) separates one segment from another". This means that you should not (or don't have to) split up your string based on the segment names, but on the CR separator. This makes it a bit simpler to deal with.

Soren Madsen
Share this answer
Have a look into[^]
string[] separators = {"MSH", "PID"};
string value = "MSH|^~ ... ....";
string[] words = value.Split(separators, StringSplitOptions.RemoveEmptyEntries);

PS: I hope you are aware of PHI
Share this answer
Member 10350030 21-Oct-13 12:59pm    
Thanks Rajan, But I am still confused with this solution, it will be great if you could provide a explanation.
Sergey Alexandrovich Kryukov 21-Oct-13 13:18pm    
I would ignore this request from OP; further explanation would be pointless. OP should read original documentation of the methods you use. (The only thing you could add would be the links to MSDN articles. I voted 5, anyway, as OP could easily find them.)
Ranjan.D 21-Oct-13 13:23pm    
I agree with you. Some time people are lazy to Google/Read MSDN. I feel sad :(
Member 10350030 21-Oct-13 13:08pm    
I want to devide this HL7 message as each segment in new line.
Ranjan.D 21-Oct-13 13:18pm    
Sorry updated code, your separators should be (MSH,PID, NK1, PV1 ) etc.. String.Split takes one or string separators for splitting. Don't forget to append words like below

Dim index As Integer = 0
For Each word In words
word = separators(index) & word
index = index + 1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900