0

I'm trying to get some field value from a text file using a streamReader.

To read my custom value, I'm using split() method. My separator is a colon ':' and my text format looks like:

Title: Mytitle 
Manager: Him 
Thema: Free 
.....
Main Idea: best idea ever 
.....

My problem is, when I try to get the first field, which is title, I use:

 string title= text.Split(:)[1];

I get title = MyTitle Manager

instead of just: title= MyTitle. Any suggestions would be nice.


My text looks like this:

My mail : ........................text............

Manager mail : ..................text.............

Entity :.......................text................

Project Title :...............text.................

Principal idea :...................................

Scope of the idea : .........text...................

........................text...........................

Description and detail :................text.......
..................text..... 
Cost estimation :..........
........................text...........................
........................text...........................
........................text...........................

Advantage for us :.................................
.......................................................

Direct Manager IM :................................
Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52
Thim
  • 1
  • 2

2 Answers2

1

Updated per your post

//I would create a class to use if you haven't
//Just cleaner and easier to read
public class Entry
{
    public string MyMail { get; set; }
    public string ManagerMail { get; set; }
    public string Entity { get; set; }
    public string ProjectTitle { get; set; }
    // ......etc
}

//in case your format location ever changes only change the index value here
public enum EntryLocation
{
    MyMail = 0,
    ManagerMail = 1,
    Entity = 2,
    ProjectTitle = 3
}

//return the entry
private Entry ReadEntry()
{
    string s =
        string.Format("My mail: test@test.com{0}Manager mail: test2@test2.com{0}Entity: test entity{0}Project Title: test project title", Environment.NewLine);

    //in case you change your delimiter  only need to change it once here
    char delimiter = ':';

    //your entry contains newline so lets split on that first
    string[] split = s.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

    //populate the entry
    Entry entry = new Entry()
    {
        //use the enum makes it cleaner to read what value you are pulling
        MyMail = split[(int)EntryLocation.MyMail].Split(delimiter)[1].Trim(),
        ManagerMail = split[(int)EntryLocation.ManagerMail].Split(delimiter)[1].Trim(),
        Entity = split[(int)EntryLocation.Entity].Split(delimiter)[1].Trim(),
        ProjectTitle = split[(int)EntryLocation.ProjectTitle].Split(delimiter)[1].Trim()
    };

    return entry;
}
Tsukasa
  • 6,342
  • 16
  • 64
  • 96
  • Thank you for your answer, but Title= Mytitle and Manager=HIM, are not in the same line. but i understand your answer :) – Thim Jul 22 '14 at 14:27
  • @Thim You post suggests otherwise. If you could post exactly how it looks then we can better assist you. – Tsukasa Jul 22 '14 at 14:34
  • @Thim so your values are each on it's own line? Show how you are reading these values please. – Tsukasa Jul 22 '14 at 14:48
  • by using for exmple for the firt value, string title= textfile.Split(:)[1]. so by changing the indexof i get value line by line. – Thim Jul 22 '14 at 14:58
  • @Thim please show me exactly what is in textfile var – Tsukasa Jul 22 '14 at 15:06
  • Thanks for help, just added class fr the code . And tins work perfectly. :) – Thim Jul 30 '14 at 11:27
-1

That is because split returns strings delimited by the sign you've specified. In your case:

Title
Mytitle Manager
Him

.1. You can change your data format to get the value you need, for example:

Title: Mytitle:Manager: Him

There each second element will be the value.

text.Split(:)[1] == " Mytitle";    
text.Split(:)[3] == " Him";

.2. Or you can call text.Split(' ', ':') to get identical list of name-value pairs without format change.

.3. Also if your data is placed each on a new line in the file like:

Title: Mytitle
Manager: Him

And you content is streamed into single string then you can also do:

text.Split(new string[] {Environment.NewLine, ":"}, StringSplitOptions.None);
Alexander Smirnov
  • 1,573
  • 12
  • 23
  • Honestly i don't get it why you -1 me. If he can change data format he can achieve the same result with only one split symbol. – Alexander Smirnov Jul 22 '14 at 14:24
  • hey Alex thank you for your answer, i'll try to change a text format. i didn't put any - 1 : ) – Thim Jul 22 '14 at 14:36
  • I think if it is a stream content and you have each of your statements on a new line you must be missing Environment.NewLine constant in your split actions. – Alexander Smirnov Jul 22 '14 at 14:38