C는

2017-02-03 1 views
1

나는이C는

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

따옴표의 각 쌍과 같은 문자열이 쉼표로 구분 된 필드가 있습니다. 문자열의 8 번째 필드를 비우고 싶습니다. 해당 필드에 아무 것도 포함될 수 있기 때문에 단순히 replace("MUL,NBLD,NITA,NUND","") 할 수 없습니다. 또한 네 번째 필드는 숫자이므로 5 주위에 작은 따옴표가 없습니다.

어떻게하면됩니까?

+0

_empty_라고 말하면 8 번째 칸을 안에 넣지 않고 두 칸을 쉼표로 구분하거나 완전히 없애려고합니까? – Steve

답변

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

빠른 답변을 보내 주셔서 감사합니다. 그게 효과가 있었어! – Shawn

1

아래에 작은 구현을 만들었습니다. 논평에서 논리를 설명합니다. 기본적으로 간단한 파서를 작성하여 설명 된 것을 성취하고자합니다.

edit0는 : 쉼표로 구분 된 목록에서 전체 필드를 제거 반대로 널 (null)로 문자열을 교체 : 그냥 내가 지금 oops..fixed

EDIT1 요청 것과 반대했다 깨달았다.

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

사용한다면 '-'필드 (시험 : 'MUL-NBLD - 니타 - NUND')의 내부 대신 (또는 다른 문자) ',',이 코드를 사용할 수 있습니다

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'"