简体   繁体   English

.net 4.0中的字符串操作

[英]String Manipulation in .net 4.0

I have been out of touch with programming for a while. 我已经与编程脱节了一段时间。 I have some strings I want to format. 我有一些我想格式化的字符串。 They look like this: 它们看起来像这样:

,SUM(CASE WHEN [Level] IN('4') AND Program = 1 THEN 1 ELSE 0 END) as CNT1
,SUM(CASE WHEN [Level] IN('3') AND Program = 1 THEN 1 ELSE 0 END) as CNT2
,SUM(CASE WHEN [Level] IN('2') AND Program = 1 THEN 1 ELSE 0 END) as CNT3
,SUM(CASE WHEN [Level] IN('1') AND Program = 1 THEN 1 ELSE 0 END) as CNT4
,SUM(CASE WHEN [Level] IN('0') AND Program = 1 THEN 1 ELSE 0 END) as CNT5

I want to take this and change it to 我想把它改成它

,CNT1 = SUM(CASE WHEN [Level] IN('4') AND Program = 1 THEN 1 ELSE 0 END) 
,CNT2 = SUM(CASE WHEN [Level] IN('3') AND Program = 1 THEN 1 ELSE 0 END) 
,CNT3 = SUM(CASE WHEN [Level] IN('2') AND Program = 1 THEN 1 ELSE 0 END) 
,CNT4 = SUM(CASE WHEN [Level] IN('1') AND Program = 1 THEN 1 ELSE 0 END) 
,CNT5 = SUM(CASE WHEN [Level] IN('0') AND Program = 1 THEN 1 ELSE 0 END) 

right now I have a multiline textbox that I paste the strings in. I want to click a button and have it formatted into a second textbox. 现在我有一个multiline textbox ,我粘贴了字符串。我想点击一个按钮,并将其格式化为第二个文本框。

This is what I've tried so far. 这是我到目前为止所尝试的。 but I am not sure what string functions to use format it the way i want. 但我不确定使用什么字符串函数格式化我想要的方式。

            List<String> Items = new List<string>();
            string frTxt;
            foreach (string lne in txtM.Lines)
            {
                frTxt = "";
                frTxt = lne;

            }  

where txtM is the multiline textbox. 其中txtM是多行文本框。 If I do Console.WriteLine(lne); 如果我做Console.WriteLine(lne); in the loop, it loops through successfully. 在循环中,它成功循环。

Assuming the format is always 假设格式总是如此

,<text_1> as <text_2> ,<text_1>为<text_2>

This the following will transform it to: 以下将把它转换为:

,<text_2> = <text_1> ,<text_2> = <text_1>

txtM.Lines = txtM.Lines.Select(l => Regex.Replace(l, ",(.+) as (.+?)$", ",$2 = $1")).ToArray();

Explanation of regex: 正则表达式的解释:

  • The "," and " as " match those exact strings “,”和“as”匹配那些确切的字符串
  • "(.+)" greedy-matches at least one character, and captures it as the first group “(。+)”贪婪地匹配至少一个角色,并将其捕获为第一组
  • "(.+?)" non-greedy-matches at least one character and captures it as the second group “(。+?)”非贪婪匹配至少一个字符并将其捕获为第二组
  • "$" matches the end of the line “$”匹配行尾

As a result of combining the greedy and non-greedy matching, we force the " as ..." to be the last occurrence (so if the string happens to contain " as " somewhere in it, that won't matter). 由于结合了贪婪和非贪婪的匹配,我们强制“as ...”成为最后一次出现(所以如果字符串碰巧在其中的某处包含“as”,则无关紧要)。 By adding the "$" we ensure that the second capture takes everything to the end of the line (otherwise as it's a non-greedy match it would match just the first character after " as ", which isn't what we want). 通过添加“$”,我们确保第二次捕获将所有内容都带到行尾(否则因为它是非贪婪的匹配,它只匹配“as”之后的第一个字符,这不是我们想要的)。

The replacement simply constructs the new string using the two groups captured by the regex, adding the "=". 替换只是使用正则表达式捕获的两个组构造新字符串,添加“=”。 We also manually add back the "," at the beginning because our regex matches on it and so it will be replaced by the replacement string. 我们还在开始时手动添加“,”,因为我们的正则表达式匹配它,因此它将替换为替换字符串。 (This can be overcome but would probably make the regex more complex). (这可以克服,但可能会使正则表达式更复杂)。

well, if it's just the beginning and end there, you can do 好吧,如果它只是开始和结束那里,你可以做到

myString.Replace(",", ",CNT1 = ").Replace(" as CNT1", "");

There are a lot of ways to do this with the built in string methods 使用内置string方法有很多方法可以做到这一点

Check out the String documentation 查看String文档


Here's a fully functional and tested code snipit: 这是一个功能齐全且经过测试的代码片段:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] strArr = 
            {
            ",SUM(CASE WHEN [Level] IN('4') AND Program = 1 THEN 1 ELSE 0 END) as CNT1",
            ",SUM(CASE WHEN [Level] IN('3') AND Program = 1 THEN 1 ELSE 0 END) as CNT2",
            ",SUM(CASE WHEN [Level] IN('2') AND Program = 1 THEN 1 ELSE 0 END) as CNT3",
            ",SUM(CASE WHEN [Level] IN('1') AND Program = 1 THEN 1 ELSE 0 END) as CNT4",
            ",SUM(CASE WHEN [Level] IN('0') AND Program = 1 THEN 1 ELSE 0 END) as CNT5",
            };

            //intellisence didn't like assignment in the foreach loop
            for(int i=0; i<strArr.Length; i++)
            {
                string cnt = Regex.Match(strArr[i], @"CNT\d+$").Value;
                strArr[i] = strArr[i].Replace(",", String.Format(",{0} = ",cnt)).Replace(String.Format(" as {0}", cnt), "");
            }

            foreach(string str in strArr)
            {
                Console.WriteLine(str);
            }

        }
    }
}

Check out Regex.Replace with regular expressions matching and Regex language . 查看正则表达式匹配和Regex语言的 Regex.Replace

You want to use grouping constructs (...) to capture place where to insert substring and second one to capture substring to insert. 您希望使用分组构造(...)来捕获插入子字符串的位置,并使用第二个来捕获要插入的子字符串。

This is very specific. 这是非常具体的。 Is your input string always going to have that format? 您的输入字符串是否始终具有该格式? In that case, you can use something like this: 在这种情况下,您可以使用以下内容:

        string s = textBox1.Text;
        var result = s.Split(',').ToList();
        result.ForEach(x =>
        {
            int removeAfter = x.LastIndexOf(" as CNT");
            string cntNum = x.Substring(x.LastIndexOf("CNT"), x.Length - 1);
            x = string.Format(",{0} = {1}", cntNum, x.Substring(0, removeAfter));
        });

But I strongly suggest looking into using regular expressions to search and replace. 但我强烈建议使用正则表达式来搜索和替换。

I am assuming your are using a richTextbox with multines please seeexample how you can do it 我假设您正在使用带有多线程的richTextbox,请参阅示例,了解如何执行此操作

private void FormatLinesInTextBox()
{
    int cnt = 1;
    foreach (string line in richTextBox1.Lines)
    {
        string cntNumber = cntNumber = "CNT" + cnt;
        richTextBox1.Lines[cnt - 1] = "," + cntNumber + line.Replace(cntNumber, string.Empty);
        cnt++;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM