Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 启用部分名称输入以返回c中的值#_C#_Web Services - Fatal编程技术网

C# 启用部分名称输入以返回c中的值#

C# 启用部分名称输入以返回c中的值#,c#,web-services,C#,Web Services,我有一个c#web服务,它接受一个字符串输入,并检查输入与一个充满字符串的文本文档 它的工作原理如下,假设我在输入中输入“Australia”,服务将返回“Australia”。但是,如果我还输入了Aus(或Aus,目前使其不区分大小写),它也应该返回“Australia” 另一方面,如果我输入“tra”,它不应该返回澳大利亚,只返回前3个索引为“tra”的字符串。(如果是Ch,它应该返回中国、乍得等) 目前我的代码看起来像 public String countryCode(String i

我有一个c#web服务,它接受一个
字符串
输入,并检查输入与一个充满
字符串的文本文档

它的工作原理如下,假设我在输入中输入“Australia”,服务将返回
“Australia”
。但是,如果我还输入了Aus(或Aus,目前使其不区分大小写),它也应该返回
“Australia”

另一方面,如果我输入“tra”,它不应该返回澳大利亚,只返回前3个索引为“tra”的字符串。(如果是Ch,它应该返回中国、乍得等)

目前我的代码看起来像

 public String countryCode(String input)
    {
        StringBuilder strings = new StringBuilder("", 10000);
        String text = System.IO.File.ReadAllText(Server.MapPath("countryCodes.txt"));
        String[] countries = Regex.Split(text, "#");


        int v;
        for (v = 0; v < countries.Length; v++)
        {
            if (countries[v].ToUpper().Contains(input) || countries[v].ToLower().Contains(input))
            {
                bool c = countries[v].ToUpper().Contains(input);
                bool b = countries[v].ToLower().Contains(input);
                if (b == true || c == true)
                {
                        strings.Append(countries[v] + " ");
                  }



                else
                {
                    strings.Append("Country not found");
                    break;
                }

            }



        }
        String str = strings.ToString();
        return str;
    }
公共字符串countryCode(字符串输入)
{
StringBuilder字符串=新的StringBuilder(“,10000);
String text=System.IO.File.ReadAllText(Server.MapPath(“countryCodes.txt”);
字符串[]countries=Regex.Split(文本“#”);
INTV;
对于(v=0;v
这是一个开始,但是我在比较字符串的索引时遇到了麻烦

我的问题是如何构造一些东西来检查
国家[v][0]
输入[0]
,如果是相同的,则检查
[1]
[1]
,依此类推,直到它们不相同或输入
为止。如果超出长度,则返回适当的值

如有必要,请提供澄清意见


关于

我认为您的循环可以简化为:

var valids = new List<String>();
foreach(String c in countries)
   if(c.ToUpper().StartsWith(input.ToUpper()))
       valids.Add(c);

return (valids.Any()) ? String.Join(",",valids) : "No Matches";

有没有理由不使用Startswith方法来查看字符串是否以特定模式开头?即使是用java编程,也没有意识到有一个Startswith方法:谢谢你,这要简洁得多……干杯,我不知道Startswith方法,多亏了Jonesy和R0MANARMY
var valids = countries.Select(c => c.ToUpper().StartsWith(input.ToUpper())).ToList();
return (valids.Any()) ? String.Join(",",valids) : "No Matches";