【问题标题】:how to find string from array and assign it to a string in c# asp.net [closed]如何从数组中查找字符串并将其分配给c#asp.net中的字符串[关闭]
【发布时间】:2015-02-25 09:49:12
【问题描述】:

我必须设计一个接口来读取字符串并在给定字符串中找到(m、n 和 t 字母表)中任何一个字符的第一次出现

输出应该是这样的

输入:电脑
输出:列表中的第一个字符是“m”,出现在“计算机”的第 3 位。它是单词中 8 个字符中的第 3 个

我的代码:

string a = TextBox1.Text;
string result = "";
int ss = a.Length;
string[] m = {"t","n","m"};
int b;

//search for m or n or t and assign it to string result

b = a.IndexOf (result) + 1;
string str = a.Substring(b-1,1);

Label2.Text = "The first character from the list is " + str +
     " occurred at position " + b.ToString() + " in "+ a +".
     it is the "+ b.ToString() + "rd of " + ss.ToString() +
     " characters in the word.";

如何搜索 m 或 n 或 t 并将其分配给字符串结果?!!!

【问题讨论】:

  • 那么问题是什么?
  • @MokshShah 如何搜索 m 或 n 或 t 并将其分配给字符串结果

标签: c# asp.net .net arrays string


【解决方案1】:

你可以试试这个:-

             string a = "Computer";
        string result = "";
        int ss = a.Length;
        string[] m = { "t", "n", "m" };
        int b=0;
        string str = "";
        for (int i = 0; i < ss;i++)
        {
            if(a[i]=='t' || a[i]=='n' || a[i]=='m')
            {
                b = i+1;
                str = a[i].ToString();

                break;
            }
        }
      Label2.Text = "The first character from the list is " + str +
            " occurred at position " + b.ToString() + " in " + a + ". it is the " + b.ToString() +
            "rd of " + ss.ToString() + " characters in the word.";

【讨论】:

  • 它有效..谢谢分配:)
  • 如果你觉得有用,请标记为答案......
【解决方案2】:

你需要使用 for 循环来获得你想要的东西。您将遍历您的数组,如果找到,只需将其打印在标签上,如果没有找到则中断标签,继续循环。

string a = TextBox1.Text;
int ss = a.Length;
string[] m = {"t","n","m"};
for(int i=0;i<m.Length;i++)
{
   int b = a.IndexOf (m[i]);
   if(b==-1)
      continue ; // IF not found instring continue with next character
   else
    {
      string str = a.Substring(b,1);
      Label2.Text = "The first character from the list is " + str+ " occurred at position " + (++b).ToString() + " in "+ a +". it is the "+ (++b).ToString() +
    "rd of " + ss.ToString() + " characters in the word.";
      break; // If character found break out of loop 
    }
  }

【讨论】:

  • 它有效,谢谢:)
  • 很高兴它成功了。编码愉快。
【解决方案3】:

你可以做的是,创建一个扩展方法,可以从给定的字符串中找到所有字符的索引并返回索引列表。 然后循环遍历列表中的所有字符并打印其索引。

试试下面的代码

string a = "Computert" ;

        int ss = a.Length;
        string[] m = {"t","n","m"};

        foreach(var charactersToFind in m)
        {
            var indexes = a.AllIndexesOf(charactersToFind);
             Label2.Text += "The first character from the list is '" + charactersToFind + "' occurred at position " + string.Join(",", indexes.ToArray()) + " in "+ a +".";
        }

这是你的扩展方法。

public static class extensions
{

    public static List<int> AllIndexesOf(this string str, string value) {
        if (String.IsNullOrEmpty(value))
            throw new ArgumentException("the string to find may not be empty", "value");
        List<int> indexes = new List<int>();
        for (int index = 0;; index += value.Length) {
            index = str.IndexOf(value, index);
            if (index == -1)
                return indexes;
            indexes.Add(index);
        }
     }
}

看看这个简单的控制台应用程序 DEMO

【讨论】:

  • 这是很好的代码,但我认为 OP 才刚刚开始使用基本循环,所以你不认为扩展方法对他来说有点复杂。而且你甚至没有提供任何解释。
  • 我认为它没有那么复杂,否则 OP 可能会要求更多说明。 :-)
【解决方案4】:

你可以有这样的东西:

           string a = textBox1.Text;
           string[] m = { "t", "n", "m" };
            int[] firstPositions = { -1, -1, -1 };

            //search for m or n or t and assign it to string result
            for (int i = 0; i < m.Length; i++)
            {
               firstPositions[i]= a.IndexOf(m[i]);
            }

            int min =a.Length;
            int firstPos = -1;
            for(int i=0;i<firstPositions.Length;i++)
            {
                if (firstPositions[i] < min && firstPositions[i] != -1)
                {
                    min = firstPositions[i];
                    firstPos = i;
                }
            }

             if(firstPos!=-1)

            label1.Text = "The first character from the list is " + m[firstPos] +
                " occurred at position " + (a.IndexOf(m[firstPos])+1) + " in " + a + ". it is the " + (a.IndexOf(m[firstPos])+1) +
                "rd of " + a.Length + " characters in the word.";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 2015-09-15
    相关资源
    最近更新 更多