字符串包含另外两个字符串 [英] String contains another two strings

查看:160
本文介绍了字符串包含另外两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果字符串包含2个或更多单词,是否可以使用contains函数?
这是我要尝试的操作:

Is it possible to have the contain function find if the string contains 2 words or more? This is what I'm trying to do:

string d = "You hit someone for 50 damage";
string a = "damage";
string b = "someone";
string c = "you";

if(d.Contains(b + a))
{   
    Console.WriteLine(" " + d);
    Console.ReadLine();
}

当我运行此命令时,控制台窗口会非常快速地关闭,而不会显示任何内容

When I run this, the console window just shuts down really fast without showing anything.

还有另一个问题:如果我想添加一个造成的损失,最简单的方法是获得该数字并将其放入 TryParse

And another question: if I for one want to add how much damage is done, what would be the easiest way to get that number and get it into a TryParse?

推荐答案

您最好打电话给包含两次,或使用自己的扩展方法来处理。

You would be better off just calling Contains twice or making your own extension method to handle this.

string d = "You hit someone for 50 damage";
string a = "damage";
string b = "someone";
string c = "you";

if(d.Contains(a) && d.Contains(b))
{
   Console.WriteLine(" " + d);
   Console.ReadLine();
}

就您的其他问题而言,您可以构建一个正则表达式来解析字符串以查找50,或者如果字符串始终相同,只需根据空格将其分割并获得第5部分。

As far as your other question, you could build a regular expression to parse the string to find 50 or if the string is always the same, just split it based on a space and get the 5th part.

这篇关于字符串包含另外两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆