2017-05-18 12 views
-1

일부 텍스트 콘텐츠가있는 파일이 있습니다. 예 : 파일 이름 = RandomText.txt I 콘텐츠 주어진 특정 인덱스 예를 추출 할 수 있도록하려는특정 색인이 지정된 파일의 텍스트 읽기

string content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"; 

int minRange = 1 
int maxRange = 10; 
int randomIndex = rnd.Next(minRange, maxRange); 
string text = File.ReadLines(RandomText.txt).Skip(randomIndex).First(); 

(전에서) (건너 뛸 생각 .. 여기

내가 원하는 exctly하고 밤은 내 시도이다 "ipsum의"반환해야 인덱스 10

에 인덱스 5에서 텍스트를 가져 여기 라인 대신에 내가 원하는 진짜로 .. 색인이 아닌) 사용

어떤 아이디어?

+0

파일의 내용에 대한 자세한 내용을 게시하십시오. 당신은 그것에 linebreaks가 있습니까? –

+0

아니요, 단지 연속 된 텍스트, 줄 바꿈 없음 –

+0

시작 색인이 5 인 경우 공백으로 시작하므로 결과는 다음과 같습니다.' "ipsu"' –

답변

0

File.ReadLines(RandomText.txt).Skip(은 여러 문자가 아닌 여러 줄을 건너 뜁니다. 기본적으로 System.IO.File.ReadAllText 메서드를 사용할 수 있습니다. 전체 파일 내용을 하나의 문자열로 반환합니다. 이제 특정 시작 인덱스에서 특정 길이의 하위 문자열을 가져올 수 있습니다.

int minRange = 1 
int maxRange = 10; 
Random rnd = new Random(DateTime.Now.Millisecond); 
int randomIndexStart = rnd.Next(minRange, maxRange); 
int randomIndexLength = rnd.Next(minRange, maxRange); 


string part = File.ReadAllText(@"C:\temp\read.txt").Substring(randomIndexStart, randomIndexLength);