Searching in a List<string> for all lines with a string less than 10 characters

huangapple go评论74阅读模式
英文:

Searching in a List<string> for all lines with a string less than 10 characters

问题

我有一个包含5列(制表符分隔)记录的文件。
我有一个公共类,在其中为每一列创建了公共字符串。
使用LINQ来查找所有第五列的行,其中字符串长度小于10个字符。

class VogueRec
{
    public string DOMAIN { get; set; }//0
    public string CLASSNAME { get; set; } //1
    public string CLASS { get; set; }//2
    public string KEYNAME { get; set; }//3
    public string KEY { get; set; }//4
}

最佳方法是如何使查询的结果填充一个新的 List<string>,从 List<string>1 的查找中,其中 KEY 的字符串长度小于10个字符,然后将其写入一个新的 List<string> 中。

var lsCorrel = new List<VogueRec>();

using (StreamReader sr = new StreamReader(fs))
{
    while ((sData = sr.ReadLine()) != null)
    {
        if ((sData.IndexOf("\t")) == -1)
            sData = sr.ReadLine();//jump over the code page in an correl export.//ignore line 1.		
        else
        {
            #region READ USER
            aSplit = sData.Split('\t');
            while (aSplit[1] == "USER")
            {
                if (aSplit[3] != "CITY")
                {
                    if (aSplit[3].IndexOf("CIT") == 0 || aSplit[3].IndexOf("VOICE") == 0)
                    {
                        lsCorrel.Add(new VogueRec()
                        {
                            DOMAIN = aSplit[0],
                            CLASS = aSplit[2],
                            KEYNAME = aSplit[3],
                            KEY = aSplit[4].Substring(Math.Max(0, aSplit[4].Length - 5))
                        });
                    }
                }
                break;
            }
            #endregion
        }
    }
}
var result = lsCorrel.Where(v => v.KEY.Length < 10);
英文:

I have a file with a 5 columns (tab delimited) record per line.
I have a public class where I created public string for each column.
Using LINQ to find all lines where the fifth column has a string less than 10 characters.

	class VogueRec
	{
		public string DOMAIN { get; set; }//0
		public string CLASSNAME { get; set; } //1
		public string CLASS { get; set; }//2
		public string KEYNAME { get; set; }//3
		public string KEY { get; set; }//4
	}

What is the best way to have the result of a query populate a new List<string> from a FindAll search of List<string>1.KEY lenght of string is < 10 then write to a new List<string>.

			var lsCorrel = new List&lt;VogueRec&gt;();

		using (StreamReader sr = new StreamReader(fs))
		{
			while ((sData = sr.ReadLine()) != null)
			{
				if ((sData.IndexOf(&quot;\t&quot;)) == -1)
					sData = sr.ReadLine();//jump over the code page in an correl export.//ignore line 1.		
				else
				{
					#region READ USER
					aSplit = sData.Split(&#39;\t&#39;);
					while (aSplit[1] == &quot;USER&quot;)
					{
						if (aSplit[3] != &quot;CITY&quot;)
						{
							if (aSplit[3].IndexOf(&quot;CIT&quot;) == 0 || aSplit[3].IndexOf(&quot;VOICE&quot;) == 0)
							{
								lsCorrel.Add(new VogueRec()
								{
									DOMAIN = aSplit[0],
									CLASS = aSplit[2],
									KEYNAME = aSplit[3],
									KEY = aSplit[4].Substring(Math.Max(0, aSplit[4].Length - 5))
								});
							}
						}
						break;
					}
					#endregion
				}
			}
		}
		var result = lsCorrel.Where(v =&gt; v.KEY.Length &lt; 10);

答案1

得分: 1

list.Where(v => v.KEY.Length < 10)也许?你目前有什么进展? - Charlieface 5小时前 有效。这是我的案例的答案。

英文:

list.Where(v => v.KEY.Length < 10) maybe? What do you have so far? –
Charlieface
5 hours ago
worked. This is the answer my case.

答案2

得分: 0

你首先逐行读取数据,并将键值小于10的内容放入一个列表中。

列分隔符:制表符("\t")
行分隔符:换行

public class TestReadFile
{
    public List<VogueRec> Get()
    {
        string path = @"D:\f.txt";
        List<VogueRec> VogueRecs = new List<VogueRec>();
        using (StreamReader sr = new StreamReader(path))
        {
            while (sr.Peek() >= 0)
            {
                string str;
                string[] strArray;
                str = sr.ReadLine();

                strArray = str.Split('\t');

                if (strArray[4].Length < 10)
                {
                    VogueRecs.Add(new VogueRec()
                    {
                        DOMAIN = strArray[0], CLASSNAME = strArray[1], CLASS = strArray[2], KEYNAME = strArray[3], KEY = strArray[4] 
                    });
                }
            }
        }
        return VogueRecs;
    }
}

使用方式:

TestReadFile testReadFile = new TestReadFile();
var _list= testReadFile.Get();

示例:

fdfsf	sdsds	dddd	dddd	142425414251
asas	ddd	    ssss	ssss	14200
asa	    sss	    dddd	222	    ssss
asa	    sss	    dd22d	sss	    14501235647

结果:

asas	ddd	    ssss	ssss	14200
asa	    sss	    dddd	222	    ssss
英文:

You first read the data line by line and put the key value less than 10 in a list

column separator: Tab("\t")
Line separator: line

 public class TestReadFile
    {
        public List&lt;VogueRec&gt; Get()
        {
            string path = @&quot;D:\f.txt&quot;;
            List&lt;VogueRec&gt; VogueRecs = new List&lt;VogueRec&gt;();
            using (StreamReader sr = new StreamReader(path))
            {
                while (sr.Peek() &gt;= 0)
                {
                    string str;
                    string[] strArray;
                    str = sr.ReadLine();

                    strArray = str.Split(&#39;\t&#39;);
                    
                    if (strArray[4].Length &lt; 10)
                    {
                        VogueRecs.Add(new VogueRec() { 
                            DOMAIN = strArray[0], CLASSNAME = strArray[1], CLASS = strArray[2], KEYNAME = strArray[3], KEY = strArray[4] 
                        });

                    }

                }

            }
            return VogueRecs;
        }


    }


Use

TestReadFile testReadFile = new TestReadFile();
var _list= testReadFile.Get();

Example

> fdfsf sdsds dddd dddd 142425414251
> asas ddd ssss ssss 14200
> asa sss dddd 222 ssss
> asa sss dd22d sss 14501235647

Result

> asas ddd ssss ssss 14200
> asa sss dddd 222 ssss

huangapple
  • 本文由 发表于 2023年6月15日 05:49:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定