在 C# 中使用 Linq 以不同条件分割字符串
我需要从字符串中提取并删除一个单词。该单词应为大写,并位于分隔符 /
、;
、(
、-
或
“
一些示例:
- 这是测试 A/ABC”
预期输出:“这是测试 A”
和“ABC”
“这是测试;ABC/XYZ”
预期输出:“这是一个测试;ABC”
和“XYZ”
“此任务已分配给我们项目中的 ANIL/SHAM”
预期输出:“此任务已分配给我们项目中的 ANIL”
和“SHAM”
“此任务已分配给我们项目中的 ANIL/SHAM” “
预期输出:“此任务已分配给项目中的 ANIL/SHAM”
和“OUR”
“这是测试 AWN.A”
预期输出:“这是测试”
和“AWN.A”
“XETRA-DAX”
预期输出:"XETRA"
和"DAX"
"FTSE-100"
预期输出:"-100"
和"FTSE"
"ATHEX"
预期输出:""
和"ATHEX"
"Euro-Stoxx-50"
预期输出:"Euro-Stoxx-50"
和""
我怎样才能实现这一目标?
I need to extract and remove a word from a string. The word should be upper-case, and following one of the delimiters /
, ;
, (
, -
or a space.
Some Examples:
"this is test A/ABC"
Expected output:"this is test A"
and"ABC"
"this is a test; ABC/XYZ"
Expected output:"this is a test; ABC"
and"XYZ"
"This TASK is assigned to ANIL/SHAM in our project"
Expected output:"This TASK is assigned to ANIL in our project"
and"SHAM"
"This TASK is assigned to ANIL/SHAM in OUR project"
Expected output:"This TASK is assigned to ANIL/SHAM in project"
and"OUR"
"this is test AWN.A"
Expected output:"this is test"
and"AWN.A"
"XETRA-DAX"
Expected output:"XETRA"
and"DAX"
"FTSE-100"
Expected output:"-100"
and"FTSE"
"ATHEX"
Expected output:""
and"ATHEX"
"Euro-Stoxx-50"
Expected output:"Euro-Stoxx-50"
and""
How can I achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“智能”版本:
“愚蠢的 LINQ”版本:
两种情况都没有检查。如果 OP 需要的话,他可以添加支票。
对于第二个问题,使用 LINQ 确实太困难了。使用正则表达式,这“很容易实现”。
对于第三个问题,
带有代码示例: http://ideone.com/5OSs0
另一个更新(它变得无聊)
两者之间的区别是第一个将使用 AZ 作为大写字符,第二个将使用其他“大写”字符,例如
ÀÈÉÌÒÙ
代码示例: http://ideone.com/FqcmY
An "intelligent" version:
A "stupid LINQ" version:
both cases are WITHOUT checks. The OP can add checks if he wants them.
For the second question, using LINQ is REALLY too much difficult. With a Regex it's "easily doable".
For the third question
With code sample: http://ideone.com/5OSs0
Another update (it's becoming BORING)
The difference between the two is that the first will use A-Z as upper case characters, the second one will use other "upper case" characters, for example
ÀÈÉÌÒÙ
With code sample: http://ideone.com/FqcmY
这应该根据新的要求工作:它应该找到最后一个用大写单词包裹的分隔符:
这个正则表达式有点棘手。主要技巧:
RegexOptions.RightToLeft
查找最后一个匹配项。$`$'
作为替换字符串: http://www.regular- Expressions.info/refreplace.html\p{Lu}
表示大写字母,如果您更习惯,可以将其更改为[AZ]
。如果该单词不应跟随大写单词,您可以将正则表达式简化为:
如果您还想要其他字符,您可以使用字符类(并且可能删除
\b
)。例如:工作示例:http://ideone.com/U9AdK
This should work according to the new requirements: it should find the last separator that is wrapped with uppercase words:
This regex is a little tricky. Main tricks:
RegexOptions.RightToLeft
to find the last match.$`$'
as replacement string: http://www.regular-expressions.info/refreplace.html\p{Lu}
for upper-case letters, you can change that to[A-Z]
if your more comfortable with that.If the word shouldn't follow an upper case word, you can simplify the regex to:
If you want other characters as well, you can use a character class (and maybe remove
\b
). For example:Working example: http://ideone.com/U9AdK
使用字符串列表,将所有单词设置为它
找到
/
的索引,然后使用ElementAt()
确定要拆分的单词,即“SHAM”问题。在下面的句子中,您的索引
/
将为 6。然后在
index
末尾使用ElementAt(6)
是索引
List
中的/
这将返回 SHAM
则只需打印不带 SHAM 的 strSentence
,这将删除 SHAM,然后如果您不想使用字符串列表, 我认为你可以使用“”来确定句子中的单词,但这还有很长的路要走。
我认为我的想法是正确的,但代码可能不是那么完美。
use a List of strings, set all the words to it
find the index of the
/
then useElementAt()
to determine the word to split which is "SHAM" in your question.in the below sentence of yours your index of
/
will be 6.then use
ElementAt(6)
at the end ofindex
is the index of the/
in yourList<string>
this will return you the SHAM
this will delete the SHAM then just print the strSentence without SHAM
if you dont want to use a list of strings you can use the " " to determinate the words in your sentence i think, but that would be a long way to go.
the idea of mine is right i think but the code may not be that flawless.
您可以结合使用
string.Split()
方法和Regex
类。简单的Split
适用于简单的情况,例如根据字符/
进行拆分。正则表达式非常适合匹配更复杂的模式。You can use a combination of the
string.Split()
method and theRegex
class. A simpleSplit
is suitable for simple cases, such as splitting according to the character/
. Regular expressions are perfect for matching more complicated patterns.作为概念证明,您可以使用 TakeWhile 和 SkipWhile 在 LINQ 中重新实现 Split
我认为生成的代码非常丑陋,我希望您决定不使用 linq
As a proof of concept, you could re-implement Split in LINQ using TakeWhile and SkipWhile
I think the resulting code is so mind-blowingly ugly that I hope you'll decide not to use linq