2012-10-04 3 views
-6

문자열 A = "A : 001,002,003 | B999 | C : 002,003"아래의 형식으로 변환하려는 경우 A = "A001 | A002 | A003 | B999 | C002 | C003";문자열 조작

+5

그래서 무엇을 시도 했습니까? –

+0

1. 몇 가지 방법을 시도 했음 Like Like Before 문자열 가져 오기 : 다음에 문자열 찾기를 추가 한 다음 모두 제거 : by | 하지만 여전히. 나는 모두 한 줄로 원한다. – Nick

+6

그건 C# 코드처럼 들리지 않습니다 ... 당신이 시도한 코드 *를 보여주기 위해 질문을 편집하십시오 ... –

답변

1

100 % 확인 시작되었습니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace StringManipulation 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     string A = "A:001,002,003|B999|C:002,003"; 
     string output = ""; 

     //use pipe character as starting delimiter 
     string[] parts = A.Split('|'); 
     foreach (string s in parts) 
     { 
      //split each set by the Name you want to give it, 
      //versus the values you're attaching to that name 
      string[] nameValue = s.Split(':'); 
      //special case if only one name-value pair exists 
      if (nameValue.Length == 1) 
      { 
       output += s + "|"; 
      } 
      else if (nameValue.Length > 1) 
      { 
       string name = nameValue[0]; 
       string[] values = nameValue[1].Split(','); 
       for (int i = 0; i < values.Length; i++) 
       { 
        output += (name + values[i] + "|"); 
       } 
      } 
     }//end foreach 
     //remove final pipe character 
     if (output.Length > 0) 
     { 
      output = output.Substring(0, output.Length - 1); 
     } 
     Console.WriteLine(output); 
     Console.Read(); 
    } 
} 
} 
+0

이것은 100 % 정확합니다. 나는 그것을 원합니다. 많은 사람에게 감사합니다. – Nick

0

하나의 전체 답 : 당신이 당신이 한 줄 솔루션을 원하는 질문에 후,하지만 여기에 당신에게 가야 내가 해낸 일이 무엇

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace zielonka.co.uk.stackoverflow.StringParse 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string a = "A:001,002,003|B999|C:002,003"; 
      StringBuilder b = new StringBuilder(); 
      //seperate out the |'s 
      IList<string> main = a.Split('|'); 
      foreach (var VARIABLE in main) 
      { 
       //If there are multiple items 
       if (VARIABLE.Contains(':')) 
       { 
        //take the first letter 
        string initial = VARIABLE.Substring(0, 1); 
        string getNumberString = VARIABLE.Substring(2, VARIABLE.Length-2);//-2 = offset from start 
        //separate out the numbers 
        IList<string> numbers = getNumberString.Split(','); 
        foreach (var number in numbers) 
        { 
         if(b.Length==0)//first set 
          b.Append(initial + number); 
         else 
         {//Not first 
          b.Append("|" + initial + number); 
         } 
        } 
       } 
       else//This is a single entry 
       { 
        if (b.Length != 0) 
        { 
         b.Append("|"); 
        } 
        b.Append(VARIABLE); 
       } 
      } 
      Console.WriteLine(b); 
      Console.ReadKey(); 

     } 
    } 
} 
+0

감사합니다, 당신의 유익한 회신을 기다릴 것입니다. – Nick

+0

위와 같이 편집 됨 : –