2012-12-13 2 views
0

분할 문자열에 대한 질문이 있고 DataTable에 넣습니다. 두 번째 배열이 문자열인지 숫자인지 어떻게 알 수 있습니까?분할 문자열 및 DataTable 넣어 두 번째 인덱스 번호를 찾으십시오

text : ... 
     abc 123 def 1 \"ok lo\" ; 
     abc def 1 \"ok lo\" ; 
     ... 

배열 2 :

tmpList[0] = abc 
tmpList[1] = 123 
tmpList[2] = def 
tmpList[3] = 1 \"ok lo\" 

배열 1 : 내가 한

tmpList[0] = abc 
tmpList[1] = def 
tmpList[2] = 1 \"ok lo\ 

모든 문자열을 찾으려면 startwith ABC :

나는이 같은 많은 캐릭터와 텍스트가

 StreamReader fin = new StreamReader(userSelectedFilePath1); 
     string tmp = ""; 
     while ((tmp = fin.ReadLine()) != null) 
     { 
     if (tmp.StartsWith("abc ")) 
      { 
       var tmpList1 = tmp.Split(new[] { '"' }).SelectMany((s, i) => 
       { 
        if (i % 2 == 1) return new[] { s }; 
        return s.Split(new[] { ' ', ';', ',' }, StringSplitOptions.RemoveEmptyEntries); 
       }).ToList(); 

       table.Rows.Add(new object[] { tmpList1[0], tmpList1[1], tmpList1[2], tmpList1[3]}); 

      } 

     } 

이 코드를 사용하면 String startswith abc, split을 찾고 DataTable에 넣을 수 있습니다. 두 번째 인덱스가 문자열인지 int인지 어떻게 알 수 있습니까? 내가 무엇을했는지와 함께 나는 두 번째 색인에 오류가 있었으며 그것은 correcly가 아니었다. 나는뿐만 아니라에 대한 if(tmp.StartsWith(abc NUMBER?))else 당신 사항 String.split()를 수행 할 때

+0

귀하의 질문이 "달성하려는 대상"인 경우 누구나 쉽게 대답 할 수 있습니다. 테이블 스키마가 무엇입니까? 왜 두 번째 배열이 int 여야합니까? –

답변

0

위의 코드는 배열의 모든 값이 문자열이 될 것이다 않는 생각 ABC에 대한 예를 너무 :

tmpList[1] = 123 // this is incorrect as the value is an int 

이 해야한다 :

tmpList[1] = "123" // value is a string 

을 그럼 당신이 할 수있는 것은 int로 값을 캐스팅해야 할, 그리고 실패하면, 당신은 int로하지 알고 :

int number; 
bool result = Int32.TryParse(tmpList[1], out number); 
if (result) { 
    // your code for if the value is a number   
} 
else { 
    // your code for if the value is not a number 
} 

코드 예제는 here에서 복사되었습니다.