0

So, I've been learning C# and I need to remove everything after the ":" character.

I've used a StreamReader to read the text file, but then I can't use the Split function, then I tried it by using an int function to import it, but then again I can't use the Split function?

What I want this to do is import a text file that's written like;

name:lastname

name2:lastname2

And so that it only shows name and name2. I've been searching this for a couple of days but I can't seem to figure it out! I don't know what I'm doing wrong and how to import the text file without using StreamReader or anything else.

Edit:

I'm trying to post something to a website that goes like;

example.com/q=(name without ":")

Edit 2:

StreamReader list = new StreamReader(@"list.txt");
string reader = list.ReadToEnd();
string[] split = reader.Split(":".ToCharArray());
Console.WriteLine(split);

gives output as;

System.String[]
maccettura
  • 10,514
  • 3
  • 28
  • 35
Jeroen Velde
  • 29
  • 1
  • 4
  • I don't really have an example, I removed all the code that I used. – Jeroen Velde Feb 27 '18 at 18:33
  • Have you looked at [StreamReader.ReadToEnd](https://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend(v=vs.110).aspx)? That returns a string, which you could then use `Split` on. – Tim Feb 27 '18 at 18:34
  • Use String.indexOf and String.substring – Krzysztof Feb 27 '18 at 18:35
  • I edited it, thanks for the help. It itsn't solved yet. – Jeroen Velde Feb 27 '18 at 18:51
  • @JeroenVelde if you have 1 entry per line you can just read the line as string and use `.Replace(":", " ");` instead of split https://stackoverflow.com/a/23989887/342740 – Prix Feb 27 '18 at 18:54

2 Answers2

3

You've got a few issues here. First, use File.ReadLines() instead of a StreamReader, its much simpler and easier:

IEnumerable<string> lines = File.ReadLines("path/to/file");

Next, your lines variable needs to be iterated so you can get to each line of the collection:

foreach (string line in lines)
{
    //TODO: write split logic here
}

Then you have to split each line on the ':' character:

string[] split = line.Split(":");

Your split variable is an array of string (i.e string[]) which means you have to access a specific index of the array if you want to see its value. This is your second issue, if you pass split to Console.WriteLine() under the hood it just calls .ToString() on the object you have passed it, and with a string[] it won't automatically give you all the values, you have to write that yourself.

So if your line variable was: "name:Steve", the split variable would have two indexes and look like this:

//split[0] = "name"
//split[1] = "Steve"

I made a fiddle here that demonstrates.

maccettura
  • 10,514
  • 3
  • 28
  • 35
1

I your file size small and your name:lastname in one line use:

var lines = File.ReadAllLines("filaPath");
foreach (var line in lines)
{
     var array = line.Split(':');
     if (array.Length > 0)
     {
         var name = array[0];
      }
}

if name:lastname isn't in new line tell me how it's seprated

rahim
  • 106
  • 1
  • 11