views:

136

answers:

2

I've got this string:

UNB+UNOA:1+094200005560743089001003:OD+094200005565200077SP0001:OD+100705:1017+570180'UNH+1+SYNCRO:2::OD'MID+102711015461800+100705:1014'CDT+::::::BQ6UB'BDT+::::::1003-002'CSG+:Nisses MONTERING:::::BP2TH+005+TCSB'SEQ+CR+2059433+YV1CM5957B1574778:602816985'ARD+39855213::PART NO'SDD+100705+1'UNT+9+1'UNZ+1+570180'

This string should be divided into smaller pieces that should be stored away in different variables. The text can be divided into

  1. multiple lines by using a separator text (which is a ' character) and
  2. multiple values in each row with two types of separator text (+ sign and also : character).

The text that appears above is an order, in the form of materials, of a company (customer) to another company (supplier). When the supplier received the order in the form of a text file that is loaded into a computer program, this program splits the text up into small parts and store them in different variables which will be stored in a database at a later stage.

The purpose is to create a form using C#. This form should has a text field when the user will input the text line and then a button, once the user pushes the button the text entered into the text field will be divided into small parts and each part will be stored in a different variable. Once the text is split and stored in variables, all the data should be stored in an array or in an object (a class).


All values are stored as strings. The line consists of:

Textpart1 Textpart2 + + + + ... Textpart2 text portion (s) '

Each text portion consists of the following:

Part 1: Part2: Part 3: Part4: Part5 ... Part (n)

Each value can therefore be stated on the form

Text-> Text line-> text portion-> Del

Each text begins with a value that identifies what the information saved on the line. The first value in each row thus called for the key value (ex. MID, SEQ, ARD, etc.). Right now there are only one key value every once in each text.

Here Synroid textpart2 listed as Part 1 and the row that has the MID as the key value. SynroTid indicated that textdel3 and del2.

+2  A: 

To split a string into pieces by splitting it at a specific character, just use string.Split(). For example:

// get the order somehow
string order = ...;

var lines = order.Split('\'');     // splits at the apostrophe character

foreach (var line in lines)
{
    var pieces = line.Split('+', ':');    // splits at both of those characters

    DoSomething(pieces);
}
Timwi
thank you so much , i´ll try
A: 

As previously posted, string.Split() is the way to do it. There are several variations that I typically use:

string[] pieces = value.Split(new char[] { '+', ':' } );
string[] pieces = value.Split(new string[] { "+", ":" } ); 
string[] pieces = value.Split(new string[] { "+", ":" }, StringSplitOptions.RemoveEmptyEntries);

The third variation will remove any blank entries after the split. Useful in many cases, however, in EDI/EDIFACT you will want to maintain the blank entries as they will be needed to determine data by it's position in the segment.

Zippit