小编use*_*357的帖子

检查字符串是否包含特定单词C#

我正在检查这些字符串,看它们是否包含单词"hi",如果它们返回true则返回true.否则我会回复假.字符串"high up应返回false但返回true.我该如何解决这个问题?

    public static bool StartHi(string str)
    {            
        if (Regex.IsMatch(str, "hi"))
        {
            return true;
        }
        else
            return false;
    }

    static void Main(string[] args)
    {
        StartHi("hi there");    // -> true
        StartHi("hi");          // -> true
        StartHi("high up");     // -> false (returns true when i run)
    }
Run Code Online (Sandbox Code Playgroud)

c# regex string

3
推荐指数
1
解决办法
8547
查看次数

随机化一系列餐馆

对于我的计算机科学入门课程,我必须创建一个应用程序来选择具有各种功能的餐馆,我无法弄清楚如何随机化数组.以下是我的代码.

string[] myRestaurants = new string[9];

myRestaurants[0] = "Wendy's";
myRestaurants[1] = "Arby's";
myRestaurants[2] = "Olive Garden";
myRestaurants[3] = "The Pie";
myRestaurants[4] = "The Cheesecake Factory";
myRestaurants[5] = "Beto's";
myRestaurants[6] = "Dillinger's Saloon";
myRestaurants[7] = "Dayz Alpher";
myRestaurants[8] = "Firehouse subs";

var nextArray = myRestaurants.ToList();
Random rng = new Random();  
int n = nextArray.Count;
while (n > 1)
{
    n--;
    int k = rng.Next(n + 1);
    T value = nextArray[k];
    nextArray[k] = nextArray[n];
    nextArray[n] = value;
    //bang
}
Run Code Online (Sandbox Code Playgroud)

c# arrays random

2
推荐指数
1
解决办法
131
查看次数

标签 统计

c# ×2

arrays ×1

random ×1

regex ×1

string ×1