Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.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# - Fatal编程技术网

C# 如何获取列表中字符的位置值?

C# 如何获取列表中字符的位置值?,c#,C#,我已经尝试了几个小时,但似乎无法解决这个问题,我正在使用C#输出用户输入的字符串中每个字符的位置(在字母表中) 我有一个列表(不是数组)保存着这个叫做字母表的东西: "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" 然后我运行一个for循环 for (int i = 0; i < text.Length; i++) { in

我已经尝试了几个小时,但似乎无法解决这个问题,我正在使用C#输出用户输入的字符串中每个字符的位置(在字母表中)

我有一个列表(不是数组)保存着这个叫做字母表的东西:

"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"
然后我运行一个for循环

for (int i = 0; i < text.Length; i++)
{
    int value = alphabet.IndexOf(Convert.ToString(text[i]));
    textBox3.Text += (value);
}
for(int i=0;i
其中字母表是上面显示的列表,文本是用户输入的字符串。所以它应该在
字母表中搜索
文本[i]
中定义的特定字母的位置,对吗


相反,对于每个循环,它只是将值输出为
0
,有什么帮助吗?

我的测试应用程序显示您的代码实际工作。您确定
文本
变量的大小写与预期一致吗?奇怪的是,如果列表中不存在字符,您的文本框中就会出现“-1”,这就好像您正在发送一个“aaaaaaaa”字符串,尽管它是作为
文本
参数发送的。不管怎样,这就是证据:

using System;
using System.Collections.Generic;

namespace ConsoleApp
{
    class Program
    {
        static List<string> alphabet = new List<string>() {
            "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
        };
        static void Main(string[] args)
        {
            string text = "aaaaaaaaaaaaaaa";
            for (int i = 0; i < text.Length; i++)
            {
                int value = alphabet.IndexOf(Convert.ToString(text[i]));
                Console.Write(value);
            }
            Console.ReadLine();
        }
    }
}

您的代码似乎工作得很好。执行以下代码(稍加修改的代码版本)会将值
19,4,18,19,18,19,17,8,13,6
赋值给res

var text = "teststring";

var alphabet = new List<string>() 
    { "a", "b", "c", "d", "e", "f", "g", "h", "i",
      "j", "k", "l", "m", "n", "o", "p", "q", "r",
      "s", "t", "u", "v", "w", "x", "y", "z" };

var res = "";

foreach (char c in text)
{
    int value = alphabet.IndexOf(c.ToString());
    res += (value) + ",";
}
请尝试以下代码:

using System.Linq;

...

var chars = new []
{
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
    'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
    'q', 'r', 's', 't', 'u','v', 'w', 'x', 'y', 'z'
}.ToList();

var text = "hello";
var ids = text.Select(x => chars.IndexOf(x));
var result = string.Join(",", ids);

结果将是“7,4,11,11,14”

您确定文本包含字母表中的任何一个元素吗?
textBox3.text+=(值)不编译。您应该发布导致问题的实际代码。
var res = string.Join(",", text.Select(c => alphabet.IndexOf(c.ToString()).ToString()));
using System.Linq;

...

var chars = new []
{
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
    'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
    'q', 'r', 's', 't', 'u','v', 'w', 'x', 'y', 'z'
}.ToList();

var text = "hello";
var ids = text.Select(x => chars.IndexOf(x));
var result = string.Join(",", ids);