2013-10-14 2 views
-2

임의의 수의 줄이 포함 된 문자열 목록이 있습니다. 각 줄의 길이는 12 자이며 내용을 텍스트 파일로 인쇄하려고합니다. 이것은 지금, 나는 효과적으로 목록의 카운트 수를 두 배로, 매 6 자 후 newLine을 삽입 할문자열 목록의 모든 줄에있는 문자 사이에 개행을 추가합니다. C#

System.IO.File.WriteAllLines(@".\strings.txt", myList); 

을하고 매우 간단합니다. 당신의 세트 예컨대

System.IO.File.WriteAllLines(@".\strings.txt", myList); 
// Output from strings.txt 
123456789ABC 
123456789ABC 
// ... 

// command to insert newLine after every 6 characters in myList 
System.IO.File.WriteAllLines(@".\strings.txt", myListWithNewLines); 
// Output from strings.txt 
123456 
789ABC 
123456 
789ABC 
+1

당신이 관심이있을 수도를 in ['String.Insert (int startIndex, string value)' ] (http://msdn.microsoft.com/en-us/library/system.string.insert.aspx) – newfurniturey

+1

downvoters, downvote에 대한 이유를 추가하십시오 및 내 질문을 향상 시키려고합니다. – chwi

답변

2
System.IO.File.WriteAllLines(@".\strings.txt", myList.Select(x => x.Length > 6 ? x.Insert(6, Environment.NewLine) : x)); 

또는, 당신은 모든 라인을 알고 경우 정말 12 개 문자가 있습니다

System.IO.File.WriteAllLines(@".\strings.txt", myList.Select(x => x.Insert(6, Environment.NewLine))); 
+0

고마워요. 'x => ... '는 어떻게 작동합니까? 어느 쪽이든, 그것은 내 문제를 해결 – chwi

+0

또한 어떻게 파일에 쓰는 대신 새 목록에 저장할 수 있습니까? 대단히 감사합니다! 편집 : 추가,'ToList()'추가 – chwi

0

, 당신은 몇 가지 좋은 가정 및 인쇄 문자열을 만들 수 있습니다. 다음의 예를 생각해

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

namespace StringSplit 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string input = @"123456789ABC 
123456789ABC"; 

      string[] lines = input.Split(new char[]{'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries); 
      foreach (var l in lines) 
      { 
       System.Diagnostics.Debug.WriteLine(l.Substring(0, 6)); 
       System.Diagnostics.Debug.WriteLine(l.Substring(6, 6)); 
      } 
     } 
    } 
} 

출력 :

123456 
789ABC 
123456 
789ABC