-1

I have a file with some text content. e.g. file name = RandomText.txt

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

I want to be able to extract content given specific indexes e.g. get text from index 5 to index 10

which should return "ipsum"

Here are my attempt, which isnt doing exctly what i want..

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

(I think skip() in here used for lines rather than indexes, which isnt what i want really..)

any ideas?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
J. Doe
  • 1
  • 2

1 Answers1

1

File.ReadLines(RandomText.txt).Skip( would skip a number of lines not a number of characters. Basically you could use the method System.IO.File.ReadAllText. It will return the entire file content as one string. Now you can take a substring from a certain start index with a certain length

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); 
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76