[英]How to check for variable character in string and match it with another string of same length?
I have a rather complex issue that I'am unable to figure out.我有一个相当复杂的问题,我无法弄清楚。
I'm getting a set of string every 10 seconds from another process in which the first set has first 5 characters constant, next 3 are variable and can change.我每 10 秒从另一个进程中获取一组字符串,其中第一组的前 5 个字符是常量,接下来的 3 个是可变的并且可以更改。 And then another set of string in which first 3 are variable and next 3 are constant.然后是另一组字符串,其中前 3 个是可变的,接下来的 3 个是常量。
I want to compare these values to a fixed string to check if the first 5 char matches in 1st set of string (ABCDE*** == ABCDEFGH) and ignore the last 3 variable characters while making sure the length is the same.我想将这些值与固定字符串进行比较,以检查第一组字符串(ABCDE*** == ABCDEFGH)中的前 5 个字符是否匹配,并在确保长度相同的同时忽略最后 3 个变量字符。 Eg : if (ABCDE*** == ABCDEDEF) then condition is true, but if (ABCDE*** == ABCDDEFG) then the condition is false because the first 5 char is not same, also if (ABCDE*** == ABCDEFV) the condition should be false as one char is missing.例如:如果 (ABCDE*** == ABCDEDEF) 则条件为真,但如果 (ABCDE*** == ABCDDEFG) 则条件为假,因为前 5 个字符不相同,如果 (ABCDE*** = = ABCDEFV) 条件应该为假,因为缺少一个字符。
I'm using the * in fixed string to try to make the length same while comparing.我在固定字符串中使用 * 来尝试在比较时使长度相同。
Does this solve your requirements?这能解决您的要求吗?
private static bool MatchesPattern(string input)
{
const string fixedString = "ABCDExyz";
return fixedString.Length == input.Length && fixedString.Substring(0, 5).Equals(input.Substring(0, 5));
}
In last versions of C# you can also use ranges:在 C# 的最新版本中,您还可以使用范围:
private static bool MatchesPattern(string input)
{
const string fixedString = "ABCDExyz";
return fixedString.Length == input.Length && fixedString[..5].Equals(input[..5]);
}
BTW: You could probably achieve the same using regex.顺便说一句:您可以使用正则表达式实现相同的效果。
If the string you match against is known at compile time, your best bet is probably using regular expressions.如果您匹配的字符串在编译时是已知的,那么您最好的选择可能是使用正则表达式。 In the first case, match against ^ABCDE...$
.在第一种情况下,匹配^ABCDE...$
。 In the second case, match against ^...DEF$
.在第二种情况下,匹配^...DEF$
。
Another way, probably better if the match string is unknown, uses Length, StartsWith and EndsWith:另一种方法,如果匹配字符串未知,可能会更好,使用 Length、StartsWith 和 EndsWith:
String prefix = "ABCDE";
if (str.Length == 8 && str.StartsWith(prefix)) {
// do something
}
Then similarly for the second case, but using EndsWith instead of StartsWith.然后对于第二种情况类似,但使用 EndsWith 而不是 StartsWith。
It's always a good idea to make an abstraction.进行抽象总是一个好主意。 Here I've made a simple function that takes the pattern and the value and makes a check:在这里,我制作了一个简单的函数,它接受模式和值并进行检查:
bool PatternMatches(string pattern, string value)
{
// The null string doesn't match any pattern
if (value == null)
{
return false;
}
// If the value has a different length than the pattern, it doesn't match.
if (pattern.Length != value.Length)
{
return false;
}
// If both strings are zero-length, it's considered a match
bool result = true;
// Check every character against the pattern
for (int i = 0; i< pattern.Length; i++)
{
// Logical and the result, * matches everything
result&= (pattern[i]== '*') ? true: value[i] == pattern[i];
}
return result;
}
You can then call it like this:然后你可以这样称呼它:
bool b1 = PatternMatches("ABCDE***", "ABCDEFGH");
bool b2 = PatternMatches("ABC***", "ABCDEF");
You could use regular expressions, but this is fairly readable, RegExes aren't always.您可以使用正则表达式,但这是相当易读的,RegExes 并不总是如此。
Here is a link to a dotnetfiddle: https://dotnetfiddle.net/4x1U1E这是 dotnetfiddle 的链接: https ://dotnetfiddle.net/4x1U1E
check this检查这个
public bool Comparing(string str1, string str2)
=> str2.StartWith(str1.replace("*","")) && str1.length == str2.Length;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.