我有两个这样的字符串:
string longName = "PRODUCT MANAGER OFFICE";
string shortName = "P.M.O";
我想验证longName是否包含短名字母字符(PMO)。
但短名称可能是这样的:
string shortName = "PMO";
string shortName = "pmo";
string shortName = "pmoff";
所有这些短名称都包含p, m, o, f
个字符,这些字符也用于长名称。
所以我想要数学字符串。我怎样才能做到这一点?我应该使用正则表达式还是其他方式?
答案 0 :(得分:1)
正则表达式对于这个简单的简单任务来说是沉重而缓慢的。
string longName = "PRODUCT MANAGER OFFICE";
string shortName = "P.M.O";
public bool ValidateStrings(string longName, string shortName)
{
bool isValid = false;
foreach (var character in shortName)
{
if (Char.IsLetter(character))
{
isValid = longName.Contains<char>(character);
if (!isValid)
{
return false;
}
}
}
return isValid;
}
答案 1 :(得分:0)
这很容易。首先,将整个字符串拆分为单词:
var text = "PRODUCT MANAGER OFFICE";
string Shortform = "";
var words = text.Split(' ').ToArray();
foreach (string word in words)
{
///Now in the loop, we will get the first character of each word and pass it to the ShortForm variable
Shortform = Shortform + word.Substring(0,1);
}
所以,现在你也有了简短形式,你可以很容易地将它与给定的简短形式进行比较,样本:
if(ShortForm == "abc")
{
}
希望这会有所帮助:)
答案 2 :(得分:0)
试试这段代码......
string str = "PRODUCT MANAGER OFFICE";
string shortname = "P.M.O";
string compareStr = "";
string[] strArry = str.Split(' ');
foreach (string x in strArry)
{
if(x.Trim() != string.Empty)
{
compareStr += x[0];
}
}
//Console.WriteLine(compareStr);
if(compareStr.Equals((shortname.Replace(".", string.Empty)), StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("valid");
}
演示01:http://rextester.com/XVCG27648
演示02:http://rextester.com/GLSN75240
演示03:http://rextester.com/KVTN19152
如果输入“PMOFF”,上述方法将无效。如果您也想要,可以查看以下方法。请记住,这两种解决方案只检查所有单词的第一个字符。第二种方法只会忽略其他字符。
string str = "PRODUCT MANAGER OFFICE";
string shortname = "P.M.OFF";
int count = 0;
string compareStr = "";
string[] strArry = str.Split(' ');
foreach (string x in strArry)
{
if(x.Trim() != string.Empty)
{
count++;
compareStr += x[0];
}
}
if(compareStr.Equals((((compareStr + " ").Remove(count)).Replace(".", string.Empty)), StringComparison.InvariantCultureIgnoreCase))
{
Console.WriteLine("valid");
}
答案 3 :(得分:0)
看起来您将缩写名称与专有名称相匹配,但没有任何真正的格式(PRODUCT MANAGER OFFICE可以是pmo或pmoff)。因此,您需要能够搜索每个字符以查找匹配项,并在找不到匹配项时停止搜索。您还需要跟踪最后一个位置,以便后续重复的字符必须与新位置匹配。这使得假设订单很重要(pmo与PRODUCT MANAGER OFFICE匹配但不会与PRODUCT OFFICE MANAGER匹配)并且所有字符都很重要(pmoff与PRODUCT MANAGER OFFICE匹配但不会与PRODUCT MANAGER OF匹配)
以下内容适用于我添加的所有示例输入和一些额外的longNames测试用例。但它仍然没有防弹。正如您所看到的那样,由于第一个单词中的P,pmo也将与GYPSUM MINING OFFICE相匹配。也许你可以根据自己的需要定制它。
static void Main()
{
var longNames = new string[]
{
"PRODUCT MANAGER OFFICE",
"GYPSUM MINING OFFICE",
"The PRODUCTION of MANAGEMENT for OFFICES",
"PRODUCT PACKING PLANT",
"PRODUCT OFFICE MANAGER",
"PRODUCT MANAGER OF"
};
string shortName = "P.M.Off";
var charsToSearchFor = shortName.ToLower().Where(c => char.IsLetter(c)).ToArray();
foreach (var longName in longNames.Select(longName=>longName.ToLower()))
{
var allCharsFound = true;
var lastMatchPosition = -1;
foreach (var searchChar in charsToSearchFor)
{
var matchPosition = longName.IndexOf(searchChar, lastMatchPosition+1);
if (matchPosition > lastMatchPosition)
{
lastMatchPosition = matchPosition;
}
else if (matchPosition == -1)
{
allCharsFound = false;
break;
}
}
Console.WriteLine($"{longName} : {allCharsFound}");
}
Console.ReadKey();
}
使用P.M.Off作为shortName输入,这将输出:
PRODUCT MANAGER OFFICE : True
GYPSUM MINING OFFICE : True
The PRODUCTION of MANAGEMENT for OFFICES : True
PRODUCT PACKING PLANT : False
PRODUCT OFFICE MANAGER : False
PRODUCT MANAGER OF: False