2017-02-03 6 views
1

Ich habe Zeichenfolge wie dieseC# Zeichenfolge analysieren

string temp = "'ADDR_LINE_2','MODEL','TABLE',5,'S','Y','C40','MUL,NBLD,NITA,NUND','','Address line 2'" 

Jedes Paar Apostroph ein Feld durch Komma begrenzt ist. Ich möchte das achte Feld in der Zeichenfolge leeren. Ich kann nicht einfach replace("MUL,NBLD,NITA,NUND","") tun, weil dieses Feld irgendetwas enthalten könnte. Beachten Sie bitte auch, dass das 4. Feld eine Nummer ist und daher kein einziges Zitat um 5 ist.

Wie kann ich das erreichen?

+0

Wenn Sie _empty_ sagen wollen, wollen Sie ein achtes Feld, das durch zwei Kommas begrenzt wird, ohne dass etwas darin enthalten ist, oder möchten Sie es vollständig entfernen? – Steve

Antwort

4
static void Main() 
{ 
    var temp = "'ADDR_LINE_2','MODEL','TABLE',5,'S','Y','C40','MUL,NBLD,NITA,NUND','','Address line 2'"; 

    var parts = Split(temp).ToArray(); 
    parts[7] = null; 
    var ret = string.Join(",", parts); 

    // or replace the above 3 lines with this...   
    //var ret = string.Join(",", Split(temp).Select((v,i)=>i!=7 ? v : null)); 

    //ret == "'ADDR_LINE_2','MODEL','TABLE',5,'S','Y','C40',,'','Address line 2'" 
} 

public static IEnumerable<string> Split(string input, char delimiter = ',', char quote = '\'') 
{ 
    string temp = ""; 
    bool skipDelimiter = false; 

    foreach (var c in input) 
    { 
     if (c == quote) 
      skipDelimiter = !skipDelimiter; 
     else if (c == delimiter && !skipDelimiter) 
     { 
      //do split 
      yield return temp; 
      temp = ""; 
      continue; 
     } 

     temp += c; 
    } 
    yield return temp; 
} 
+0

Danke für die schnelle Antwort. Das hat funktioniert! – Shawn

1

Ich machte eine kleine Implementierung unten. Ich erkläre die Logik in den Kommentaren. Im Grunde wollen Sie einen einfachen Parser schreiben, um das zu erreichen, was Sie beschrieben haben.

Edit0: nur erkennen ich, das Gegenteil von dem tat, was Sie für oops..fixed jetzt gefragt

edit1: die Zeichenfolge mit null ersetzt, im Gegensatz zu dem gesamten Bereich von der durch Kommata getrennte Liste zu eliminieren.

static void Main(string[] args) 
    { 
     string temp = "'ADDR_LINE_2','MODEL','TABLE',5,'S','Y','C40','MUL,NBLD,NITA,NUND','','Address line 2'"; 
     //keep track of the single quotes 
     int singleQuoteCount= 0; 
     //keep track of commas 
     int comma_count = 0; 
     String field = ""; 
     foreach (Char chr in temp) 
     { 
      //add to the field string if we are not between the 7th and 8th comma not counting commas between single quotes 
      if (comma_count != 7) 
       field += chr; 
      //plug in null string between two single quotes instead of whatever chars are in the eigth field. 
      else if (chr == '\'' && singleQuoteCount %2 ==1) 
       field += "\'',"; 

      if (chr == '\'') singleQuoteCount++; 
      //only want to add to comma_count if we are outside of single quotes. 
      if (singleQuoteCount % 2 == 0 && chr == ',') comma_count++; 

     } 
    } 
-1

Wenn Sie verwenden '-' (oder andere Zeichen) statt '' innerhalb der Felder (Prüfung: 'MUL-NBLD-NITA-Nund'), können Sie diesen Code verwenden:

static void Main(string[] args) 
    { 
     string temp = "'ADDR_LINE_2','MODEL','TABLE',5,'S','Y','C40','MUL-NBLD-NITA-NUND','','Address line 2'"; 
     temp = replaceField(temp, 8); 
    } 

    static string replaceField(string list, int field) 
    { 
     string[] fields = list.Split(','); 
     string chosenField = fields[field - 1 /*<--Arrays start at 0!*/]; 

     if(!(field == fields.Length)) 
      list = list.Replace(chosenField + ",", ""); 
     else 
      list = list.Replace("," + chosenField, ""); 

     return list; 
    } 

    //Return-Value: "'ADDR_LINE_2','MODEL','TABLE',5,'S','Y','C40','','Address line 2'" 
Verwandte Themen