C#在foreach中创建一个字符串 [英] C# make a string in a foreach

查看:97
本文介绍了C#在foreach中创建一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从文件中取一些单词并将它们放在一个新字符串中。当我必须使用kvar创建一个新字符串时,我很高兴。



我必须使用所有k值执行字符串。





谢谢



我的尝试:



 class program 
{


static void Main()
{
string [] text1 = new string [] {}; //我试着用一个字符串但是......没有
string text = System.IO.File.ReadAllText(@Dat is my file location);
List< string> list = new List< string>(); ///甚至没有列表......现在我尝试将k放到列表中并在它之后转换为字符串..但也没有:(

正则表达式模式1 =新的正则表达式((?< = ECU_ADDRESS)。*?(?=),RegexOptions.RightToLeft); //文件的开始和结束
MatchCollection allMatches1 = pattern1.Matches(text);
StringBuilder builder = new StringBuilder();
foreach(var k in allMatches1)//这里是kvar
{



控制台.WriteLine(k); //这里我想看看我所有的k
list.Add(k.ToString()); //这里我试着看看i
//在列表中有什么但是...没有
list.ForEach(Console.WriteLine);

////////////////////builder.Append(k ).Append(,);

}

解决方案

首先,ki不是单词它是正则表达式匹配类实例,因此要获得匹配的内容,您需要使用 k.Value ,而不是 k

其次,你试图在构建它的循环中编写列表 - 你最后想要的是。

第三,你有一个StringBuilder,你甚至不使用它!



看起来你只是猜测,希望它将全部起作用 - 这不是可行的策略,根本不起作用。停止猜测,并开始思考 - 计划你将要做什么,然后如果你不知道如何,开始将其分解为更小的步骤。



尝试这个:

 string text = System.IO.File.ReadAllText(@Dat is my file location); 
正则表达式pattern1 =新的正则表达式((?< = ECU_ADDRESS)。*?(?=),RegexOptions.RightToLeft); //我的文件的开始和结束
MatchCollection allMatches = pattern1.Matches(text);
StringBuilder words = new StringBuilder();
foreach(在所有匹配中匹配)
{
words.AppendLine(match.Value);
}
Console.Write(words.ToString());

但是......你的正则表达式错了。它看起来就像我昨天给你的那个,但是攻击了我不理解正则表达式的人:

(?< = ID Config:\\\\ + )\\\\ + +(?= \\ s)

您的版本没有终止条件(我的(?= \\ s)是停止在第一个空格,你的只是空的,。*?部分说绝对没有匹配,因为*表示零或更多重复任何东西,而?表示尽可能少这将永远意味着没有,请。



停止,查看您的数据,并找出确切标识的开始和结束的内容你想要捕获的文本。然后使用我昨天给你的样本和Expresso的副本来找出你需要的东西: Expresso [ ^ ] - 它是免费的,它会检查并生成正则表达式


匹配方法不返回字符串集合。它返回的是一个匹配对象的集合。

您应该访问此类对象的属性以获取所需的字符串。请参阅文档 [ ^ ]。


你用你的很多随机变量弄乱你的代码从不使用。这个问题很容易解决,因为@OriginalGriff昨天在你的问题中向你解释了 https://www.codeproject.com/Questions/1258200/How-do-I-writte-a-code-in-Csharp-for-read-some-wor [ ^ ]。



  private   static 正则表达式findID = 正则表达式(
(?< = ID Config:\\\\ +)\\\\ + +(?= \\止)
RegexOptions.Multiline
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);
...
字符串 InpuText = System.IO.File.ReadAllText( @ Dat是我的文件位置);
MatchCollection ms = findID.Matches(InputText);
foreach (匹配m in ms)
{
Console。的WriteLine(m.Value);
}





向OG道歉,要求修复他可怕的支撑缩进。 :)


I have to take some words from a file and put them in a new string. I am stuk when i have to make a new string with that "k" var.

I have to do a string with all "k" values.


Thanks

What I have tried:

class Program
   {


       static void Main()
       {
           string[] text1 = new string[] { }; //i tried with a string but... nothing
           string text = System.IO.File.ReadAllText(@"Dat is my file location");
           List<string> list = new List<string>();  ///not even with a list... now i try to put the "k" to an list and after it to convert to string.. but nothing too :(

           Regex pattern1 = new Regex("(?<=ECU_ADDRESS).*?(?=)", RegexOptions.RightToLeft);               //The start and stop of my file
           MatchCollection allMatches1 = pattern1.Matches(text);
           StringBuilder builder = new StringBuilder();
           foreach (var k in allMatches1)    //here is the "k" var
           {



               Console.WriteLine(k);     // here i want to see all my k
               list.Add(k.ToString());            //   here i tried to see what i
                                                     //   have in list but... nothing
               list.ForEach(Console.WriteLine);

               ////////////////////builder.Append(k).Append(", ");

           }

解决方案

For starters, k is not a "word" it's a regex Match class instance, so to get the content it matched, you need to use k.Value, not k
Second, you are trying to write the list inside the loop that builds it - you want that at the end.
Thirdly, you have a StringBuilder and you don't even use it!

This looks like you are just guessing, and hoping it will all work - and that isn;t a strategy that will work, not at all. Stop guess, and start thinking - plan what you are going to do, and then if you don't know how, start breaking that down into smaller steps.

Try this:

string text = System.IO.File.ReadAllText(@"Dat is my file location");
Regex pattern1 = new Regex("(?<=ECU_ADDRESS).*?(?=)", RegexOptions.RightToLeft);               //The start and stop of my file
MatchCollection allMatches = pattern1.Matches(text);
StringBuilder words = new StringBuilder();
foreach (Match match in allMatches)
    {
    words.AppendLine(match.Value);
    }
Console.Write(words.ToString());

But ... your regex looks wrong. It looks like the one I gave you yesterday, but hacked my somebody who doesn't understand regexes:

(?<=ID Config:\\s+)\\w+(?=\\s)

Your version doesn't have a terminating condition (the "(?=\\s)" in mine is a "stop at the first whitespace", yours is just empty, and the ".*?" part says "match absolutely nothing" because "*" means "zero or more repetitions of anything at all", and the "?" after it says "as few as possible" which will always means "none, please".

Stop, look at your data, and work out what exactly identifies the start and end of the text you want to capture. Then use the sample I gave you yesterday and a copy of Expresso to work out what you need: Expresso[^] - it's free, and it examines and generates Regular expressions.


The Matches method doesn't return a collection of strings. It returns instead a collection of, well, Match objects.
You should access the properties of such objects in order to obtain the strings you need. See the sample code in the documentation[^].


You are cluttering your code with a lot of random variables that you never use. The problem is quite easy to resolve as @OriginalGriff explained to you yesterday in your question at https://www.codeproject.com/Questions/1258200/How-do-I-writte-a-code-in-Csharp-for-read-some-wor[^].

private static Regex findID= new Regex(
      "(?<=ID Config:\\s+)\\w+(?=\\s)",
    RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.Compiled
    );
...
    String InpuText = System.IO.File.ReadAllText(@"Dat is my file location");
    MatchCollection ms = findID.Matches(InputText);
    foreach (Match m in ms)
    {
        Console.WriteLine(m.Value);
    }       



Apologies to OG for'fixing' his horrible braces indentation. :)


这篇关于C#在foreach中创建一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆