C# Read from text file
-
This is one part of text file: ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"] I want to get text from text file between [ and ? I tried this commands but it doesn't work:
List questions = File.ReadLines(path)
.Where(x => x.StartsWith("[") &&
x.EndsWith("?"))
.ToList();foreach (string x in questions) { listBox1.Items.Add(x); }
-
This is one part of text file: ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"] I want to get text from text file between [ and ? I tried this commands but it doesn't work:
List questions = File.ReadLines(path)
.Where(x => x.StartsWith("[") &&
x.EndsWith("?"))
.ToList();foreach (string x in questions) { listBox1.Items.Add(x); }
x does not endwith ? it contains ?
-
This is one part of text file: ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"] I want to get text from text file between [ and ? I tried this commands but it doesn't work:
List questions = File.ReadLines(path)
.Where(x => x.StartsWith("[") &&
x.EndsWith("?"))
.ToList();foreach (string x in questions) { listBox1.Items.Add(x); }
Look at each line, and use IndexOf and Substring:
string s = @"[""Koje ostrvo od Italije razdvaja Ligursko more.?"",2,""Sicilija"",""Korzika"",""Sardinija"",""Kapri""]";
int start = s.IndexOf('[') + 1;
int end = s.IndexOf('?');
string bitInTheMiddle = s.Substring(start, end - start);Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Look at each line, and use IndexOf and Substring:
string s = @"[""Koje ostrvo od Italije razdvaja Ligursko more.?"",2,""Sicilija"",""Korzika"",""Sardinija"",""Kapri""]";
int start = s.IndexOf('[') + 1;
int end = s.IndexOf('?');
string bitInTheMiddle = s.Substring(start, end - start);Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
This is one part of text file: ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"] I want to get text from text file between [ and ? I tried this commands but it doesn't work:
List questions = File.ReadLines(path)
.Where(x => x.StartsWith("[") &&
x.EndsWith("?"))
.ToList();foreach (string x in questions) { listBox1.Items.Add(x); }
StartsWith
andEndsWith
perform no operations on the strings themselves - they are simply boolean methods that return true or false based on what the string Starts or Ends with - so your code will return no results. Instead, you need to perform a "Select", and extract the part of the string you require. Two possible methods come to mind immediately:File.ReadLines(path).Select(x=>x.SubString(1, x.IndexOf("?")-1).ToList();
or
File.ReadLines(path).Select(x=>x.Split(new char[]{'[','?'}, StringSplitOption.RemoveEmptyEntries)[0]).ToList();
You could also look at using Regex, but I think it might be a bit of overkill for this case.
Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
-
StartsWith
andEndsWith
perform no operations on the strings themselves - they are simply boolean methods that return true or false based on what the string Starts or Ends with - so your code will return no results. Instead, you need to perform a "Select", and extract the part of the string you require. Two possible methods come to mind immediately:File.ReadLines(path).Select(x=>x.SubString(1, x.IndexOf("?")-1).ToList();
or
File.ReadLines(path).Select(x=>x.Split(new char[]{'[','?'}, StringSplitOption.RemoveEmptyEntries)[0]).ToList();
You could also look at using Regex, but I think it might be a bit of overkill for this case.
Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
It's working:
List questions = File.ReadLines(path)
.Select(x => x.Split(new char[] { '[', '?' }, StringSplitOptions.RemoveEmptyEntries)[0]).ToList();
foreach (var x in questions)
{
listBox2.Items.Add(x);
}How to take separatly: ,2,"Sicilija","Korzika","Sardinija","Kapri" In first listbox to put answers: 2,1,1,1 In second listbox to put chioce1: Sicilija","travama","Germanik","Japanski običaj" and same for other three choices? ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"]
-
It's working:
List questions = File.ReadLines(path)
.Select(x => x.Split(new char[] { '[', '?' }, StringSplitOptions.RemoveEmptyEntries)[0]).ToList();
foreach (var x in questions)
{
listBox2.Items.Add(x);
}How to take separatly: ,2,"Sicilija","Korzika","Sardinija","Kapri" In first listbox to put answers: 2,1,1,1 In second listbox to put chioce1: Sicilija","travama","Germanik","Japanski običaj" and same for other three choices? ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"]
How about you give it a go using the techniques I just showed you? There is enough there to do that. Also, look up the methods I demonstrated on MSDN - that'll give you added info on how it all works. We're not here to do it all for you - hopefully pointing you in the right direction will have you learning.
Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
-
How about you give it a go using the techniques I just showed you? There is enough there to do that. Also, look up the methods I demonstrated on MSDN - that'll give you added info on how it all works. We're not here to do it all for you - hopefully pointing you in the right direction will have you learning.
Cheers, Mick ------------------------------------------------ It doesn't matter how often or hard you fall on your arse, eventually you'll roll over and land on your feet.
Check out his previous questions - he doesn't seem to want to learn. :sigh:
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
It's working:
List questions = File.ReadLines(path)
.Select(x => x.Split(new char[] { '[', '?' }, StringSplitOptions.RemoveEmptyEntries)[0]).ToList();
foreach (var x in questions)
{
listBox2.Items.Add(x);
}How to take separatly: ,2,"Sicilija","Korzika","Sardinija","Kapri" In first listbox to put answers: 2,1,1,1 In second listbox to put chioce1: Sicilija","travama","Germanik","Japanski običaj" and same for other three choices? ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"]
Now that you've completely changed your requirement, it's time you started reading the documentation on these methods and started experimenting on your own on how they work. You're trying to learn by constantly asking questions that are easy to find the answers to and copying and pasting code you find on the internet. You're not making an effort to understand the code you're using. You have to start creating small programs that test the methods you're trying to use so you can see how they work. You also have to get to know how to use the debugger. It's there to debug YOU and better you're understanding of how your code works.
A guide to posting questions on CodeProject
Click this: Asking questions is a skill. Seriously, do it.
Dave Kreskowiak -
This is one part of text file: ["Koje ostrvo od Italije razdvaja Ligursko more.?",2,"Sicilija","Korzika","Sardinija","Kapri"] ["Agrostologija je nauka o ?",1,"travama","tabletama","konjima","zvezdama"] ["Sin rimskog cara Klaudija i Mesaline zvao se:",2,"Germanik","Britanik","Italik","Hispanik"] ["Šta je ofuro",1,"Japanski običaj","Kineske patike","Otkrivanje","Nemački izraz"] ["Bombardon je:",1,"muzički instrument","veliki top","vrsta aviona","vrsta slatkiša"] ["Koji filozof je rekao \"Sve teče\"?",2,"Pitagora","Heraklit","Aristotel","Sokrat"] I want to get text from text file between [ and ? I tried this commands but it doesn't work:
List questions = File.ReadLines(path)
.Where(x => x.StartsWith("[") &&
x.EndsWith("?"))
.ToList();foreach (string x in questions) { listBox1.Items.Add(x); }
I think yours is an excellent example of "agile programming"; i.e. no planning, just "coding". It is obvious the leading and trailing "[]" can be eliminated. We then wind up with a "coma-delimited" file record; which can be split using string split (6 tokens). Then it's just a question of testing the first token for an "?". But, hey, as long as we come up with the "right answer", then it's all good. Right?