简体   繁体   English

如何检查字符串中的特定后缀?

[英]How can I check for a certain suffix in my string?

I got a list of strings. 我有一个字符串清单。 And I want to check for every string in there. 我想检查其中的每个字符串。 Sometimes, a string can have the suffix _anim(X) where X is an integer. 有时,字符串可以带有后缀_anim(X) ,其中X是整数。 If such string has that kind of suffix, I need to check for all other strings that have the same "base" (the base being the part without suffix) and finally group such strings and send them to my function. 如果此类字符串具有这种后缀,则需要检查所有其他具有相同“基数”(基数是不带后缀的部分)的字符串,最后将此类字符串分组并将其发送给我的函数。

So, given the next list: 因此,给出下一个列表:

Man_anim(1) 曼妮(1)

Woman 女人

Man_anim(3) Man_anim(3)

Man_anim(2) Man_anim(2)

My code would discover the base Man has a special suffix, and will then generate a new list grouping all Man objects and arrange them depending on the value inside parenthesis. 我的代码将发现基本Man有一个特殊的后缀,然后将生成一个将所有Man对象分组的新列表,并根据括号内的值对其进行排列。 The code is supposed to return 该代码应该返回

Man_anim(1) 曼妮(1)

Man_anim(2) Man_anim(2)

Man_anim(3) Man_anim(3)

And send such list to my function for further processing. 并将这样的列表发送到我的函数进行进一步处理。

My problem is, how can I check for the existence of such suffix, and afterwards, check for the value inside parenthesis? 我的问题是,如何检查该后缀的存在,然后再检查括号内的值?

If you know that the suffix is going to be _anim(X) every time (obviously, with X varying) then you can use a regular expression: 如果您知道后缀每次都会是_anim(X) (显然, X有所不同),则可以使用正则表达式:

Regex.IsMatch(value, @"_anim\(\d+\)$")

If the suffix isn't at least moderately consistent, then you'll have to look into data structures, like Suffix Trees, which you can use to determine common structures in strings. 如果后缀至少没有一定程度的一致性,那么您将不得不研究诸如后缀树之类的数据结构,您可以使用它们来确定字符串中的常见结构。

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

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