[英]Spliting a string by capital letters except for certain keywords
The following function splits a string at any uppercase character found within the string. 以下函数将字符串拆分为该字符串内找到的任何大写字符。
public static string ToSentence(this string input)
{
var list = new List<char>();
for (var i = 0; i < input.ToCharArray().Length; i++)
{
var c = input.ToCharArray()[i];
foreach (char c1 in i > 0 && char.IsUpper(c) ? new[] {' ', c} : new[] {c})
list.Add(c1);
}
return new string(list.ToArray());
}
In my code, this function is being used in conjunction with another function that retrieves the name of the current method in code. 在我的代码中,此函数与另一个函数一起使用,该函数在代码中检索当前方法的名称。 I'm finding this function breaks when a method name contains multiple capital letters sequentially.
当方法名称顺序包含多个大写字母时,我发现此功能中断。
For example, if I have a method called GetDatabaseIDE()
it would return as "Get Database IDE"
例如,如果我有一个名为
GetDatabaseIDE()
的方法,它将返回为"Get Database IDE"
How can I change my ToSentence
function so that it accepts a list of keywords that won't be split (For example, IDE
becomes IDE
)? 如何更改
ToSentence
函数,使其接受不会被拆分的关键字列表(例如, IDE
变为IDE
)?
Why not try Regex ? 为什么不尝试Regex? Demo @ https://dotnetfiddle.net/FsPZ9O
演示@ https://dotnetfiddle.net/FsPZ9O
1. ([AZ]+)
- match all leading uppercase char. 1.
([AZ]+)
-匹配所有前导大写字符。
2. ([^AZ])*
- followed with zero-or-more of any that isn't an uppercase char. 2.
([^AZ])*
-后面跟零或更多的不是大写字符的字符。
Regex.Matches("GetDatabaseIDE", @"([A-Z]+)([^A-Z])*").Cast<Match>().Select(m => m.Value);
TakeWhile
method can be useful here, once you find an upper case character, you can take the following upper case characters: TakeWhile
方法在这里很有用,一旦找到大写字符,就可以采用以下大写字符:
for (var i = 0; i < input.Length; i++)
{
var c = input[i];
if(char.IsUpper(c))
{
var charsToAdd = input.Skip(i).TakeWhile(char.IsUpper).ToArray();
if(charsToAdd.Length > 1)
{
i += charsToAdd.Length - 1;
}
if(i > 0) list.Add(' ');
list.Add(charsToAdd);
}
else
{
list.Add(c);
}
}
you can add keywords you want to skip: 您可以添加要跳过的关键字:
public static string ToSentence(string input)
{
var list = new List<char>();
for (var i = 0; i < input.ToCharArray().Length; i++)
{
if(input.IndexOf("IDE",i,input.Length-i)==i){
list.AddRange(" IDE");
i+=2;
}
else{
var c = input.ToCharArray()[i];
foreach (char c1 in i > 0 && char.IsUpper(c) ? new[] {' ', c} : new[] {c})
list.Add(c1);
}
}
return new string(list.ToArray());
}
This will work for your example, you will have to tweak it if your method names have numbers in them 这将对您的示例有效,如果方法名称中包含数字,则必须对其进行调整
using System.Text.RegularExpressions;
public static string GetNiceName(string testString)
{
var pattern = "([A-Z][a-z]+)|([A-Z]+)";
var result = new List<string>();
foreach (Match m in Regex.Matches(testString, pattern))
{
result.Add(m.Groups[0].Value);
}
return string.Join(" ", result);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.