简体   繁体   English

从C#中的字符串中删除最后两个或多个单词

[英]Removing the last two or multiple words from a string in c#

I have string likely 我可能有绳子

select 'abc','xyz','123' Union all select 'def','wer','456' Union all

the string is generated by loop eg 该字符串是由循环生成的,例如

foreach(var item in obj)
{
  " SELECT'" + item.a + "'," + item.b + ," + item.c + "UNION ALL";
}

Now i want to remove the "Union all" written in the last of the string.How can i do this is single OR using regex in C# .IndexOf wont work as i have multiple "Union all" in my string . 现在我想删除"Union all"写在最后的string.How的我能做到这一点是单一的或者使用C#.IndexOf正则表达式不会工作,因为我有多个"Union all"在我的字符串。

As far as I can see, you are trying to build finalQuery from several select s; 据我所知,您正在尝试从多个select构建finalQuery you can do it with a help of Join : 您可以在Join的帮助下做到这一点:

  string[] selects = new string[] {
    "select 'abc','xyz','123'",
    "select 'def','pqr','456'", 
  };

  // select 'abc','xyz','123' Union all select 'def','pqr','456' 
  string finalQuery = string.Join(" Union all ", selects);

If you, however, want to remove the very last Union all if it is, you can test the string with EndsWith : 但是,如果要删除最后一个Union all ,则可以使用EndsWith测试字符串:

  string finalQuery = myString.EndsWith("Union all") 
    ? myString.Substring(0, myString.Length - "Union all".Length)
    : myString;

Edit: If you generate your selects in a loop (see comments below), you can try extracting method turning loop into IEnumerable<String> : 编辑:如果在循环中生成selects (请参见下面的注释),则可以尝试提取loop转换为IEnumerable<String>

   private IEnumerable<string> MySelects() {
     foreach(var item in obj) {
       // Some Logic Here...

       // When you are ready to create a select just "yield return" it and keep looping
       yield return $" SELECT '{item.a}', '{item.b}', '{item.c}'";

       // Some Other Logic Here...
     } 
   }

and then again Join : 然后再次Join

   string finalQuery = string.Join(" Union all ", MySelects());

Finally, if you want to stick to loop (for whatever reason), add if : 最后,如果您想循环播放(无论出于何种原因),请添加if

   StringBuilder sb = new StringBuilder();

   foreach(var item in obj) {
     // if we have a query, next one should be add via "UNION ALL"
     if (sb.Length > 0)
       sb.Append(" UNION ALL ");

     sb.Append($"SELECT '{item.a}', '{item.b}', '{item.c}'");
   }

   string finalQuery = sb.ToString();

You can combine string.Join and LINQ Skip : 您可以结合使用string.Join和LINQ Skip

string input = "select 'abc','xyz','123' Union all select 'def','wer','456' Union all";

string result = string.Join(" ", input.Split(' ').Reverse().Skip(2).Reverse());

DEMO HERE 此处演示

I would start with somelink like this. 我将从这样的somelink开始。 In case your replacement gets more complicated you can start using regular expressions. 如果替换变得更加复杂,则可以开始使用正则表达式。

var toRemove = "Union all";
if (yourString.EndWith(toRemove ))
{
  yourString = yourString.SubString(0, yourString.Length - toRemove.Length).Trim();
}

PS Get the toRemove string from some logical location and don't use it in your procedure like this to avoid using magic values.. PS从某个逻辑位置获取toRemove字符串,请勿在您的过程中使用该字符串,以避免使用魔术值。

You can use this as a function; 您可以将此用作函数;

var toRemove = "Union all";

if (string.IsNullOrWhiteSpace(yourString))
return;

var pos = yourString.LastIndexOf(stringToFind);

 if (pos < 0) return;

string newString = yourString.Remove(pos, yourString.Length);

return newString;

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

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