使用不带文本限定符的split()方法

本文关键字:split 方法 文本 | 更新日期: 2023-09-27 18:14:15

我正试图从使用streamReader的文本文件中获得一些字段值。

读取我的自定义值,我使用split()方法。分隔符是冒号":",文本格式如下:

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

我的问题是,当我试图获得第一个字段,这是标题,我使用:

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

我得到title = MyTitle Manager

而不仅仅是:title= MyTitle.任何建议都很好。


我的文本是这样的:

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 :................................

使用不带文本限定符的split()方法

按帖子更新

//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;
}

这是因为split返回由您指定的符号分隔的字符串。在你的例子中:

Title
Mytitle Manager
Him

。1。您可以更改数据格式以获得所需的值,例如:

Title: Mytitle:Manager: Him

每隔一个元素的值。

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

。2。或者您可以调用text。拆分(' ',':')在不更改格式的情况下获得相同的名称-值对列表。

。3。同样,如果您的数据被放置在文件中的每个新行,如:

Title: Mytitle
Manager: Him

你的内容被流化为单个字符串然后你还可以做:

text.Split(new string[] {Environment.NewLine, ":"}, StringSplitOptions.None);