Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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
BEST way prompt user 4.txt,读取它,用“,”分隔&引用;,重复2次,用“找到第一个str”;“A”&引用;“a”;,相同的4“;",&引用;z";在C#中?_C# - Fatal编程技术网

BEST way prompt user 4.txt,读取它,用“,”分隔&引用;,重复2次,用“找到第一个str”;“A”&引用;“a”;,相同的4“;",&引用;z";在C#中?

BEST way prompt user 4.txt,读取它,用“,”分隔&引用;,重复2次,用“找到第一个str”;“A”&引用;“a”;,相同的4“;",&引用;z";在C#中?,c#,C#,我一整天都在解决这个问题,似乎我没有什么进展。我在编程方面非常新,但在理解和解释方面做得很好(我认为无论如何)。我在这台电脑前花了12个小时试图弄明白这一点,我累了,大脑需要休息。任何帮助都将不胜感激。我无法逾越的部分是找出如何根据每个字符串的第一个字符做出决定。我试过用char,char[],strings,string[]进行IndexOf,混合mashin,不止一次,除了正确的方法之外,我尝试了所有方法。我尝试了StartsWith,子串都尝试了最有可能的每一件事,但它是如何完成的。请帮忙

我一整天都在解决这个问题,似乎我没有什么进展。我在编程方面非常新,但在理解和解释方面做得很好(我认为无论如何)。我在这台电脑前花了12个小时试图弄明白这一点,我累了,大脑需要休息。任何帮助都将不胜感激。我无法逾越的部分是找出如何根据每个字符串的第一个字符做出决定。我试过用char,char[],strings,string[]进行IndexOf,混合mashin,不止一次,除了正确的方法之外,我尝试了所有方法。我尝试了StartsWith,子串都尝试了最有可能的每一件事,但它是如何完成的。请帮忙

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
    {
        string files = openFileDialog1.FileName;
    }

    public void button1_Click(object sender, EventArgs e)
    { 
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                //Create string of all text in file
                string someText = File.ReadAllText(openFileDialog1.FileName);
                //Turn someText into a char arra
                char[] array = someText.ToCharArray();
                //Define vowels in a char array
                char[] vowels = { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' };
                //Converts someText to lowercase
                string lower = someText.ToLower();
                //Shows the lowercase text
                char[] charSeparators = new char[] { ',' };
                string[] split = 
                someText.Split(charSeparators,StringSplitOptions.RemoveEmptyEntries);
                string result = split.ToString(); 
                int watch = result[0];
                int at = 0;
                int end = lower.Length;
                int start = 0;
                int count;
                while ((start <= end) && (at >= -1))
                {
                    // start+count must be a position within -str-.
                    count = start;
                    at = lower.IndexOf("a", start, count);
                    start = 0;
                    MessageBox.Show("{0} ", at.ToString());
                                    
                    break;
                }   
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n" +
                $"Details:\n\n{ex.StackTrace}");
            }
        }
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
私有void openFileDialog1\u FileOk(对象发送方,CancelEventArgs e)
{
字符串文件=openFileDialog1.FileName;
}
公共无效按钮1\u单击(对象发送者,事件参数e)
{ 
如果(openFileDialog1.ShowDialog()==DialogResult.OK)
{
尝试
{
//创建文件中所有文本的字符串
string someText=File.ReadAllText(openFileDialog1.FileName);
//将某些文本转换为字符数组
char[]数组=someText.ToCharArray();
//在字符数组中定义元音
char[]元音={'a','a','e','e','i','i','o','o','u','u'};
//将某些文本转换为小写
string lower=someText.ToLower();
//显示小写文本
char[]charSeparators=新的char[]{',};
字符串[]拆分=
拆分(字符分隔符、StringSplitOptions.RemoveEmptyEntries);
字符串结果=split.ToString();
int watch=结果[0];
int at=0;
int end=较低的长度;
int start=0;
整数计数;
而((开始=-1))
{
//开始+计数必须是-str-内的位置。
计数=开始;
at=较低的指数(“a”,开始,计数);
开始=0;
Show(“{0}”,位于.ToString());
打破
}   
}
捕获(例外情况除外)
{
MessageBox.Show($”安全错误。\n\n错误消息:{ex.message}\n\n“+
$“详细信息:\n\n{ex.StackTrace}”);
}
}
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
这个。关闭();
}
}

如果我理解正确,您希望:

  • 提示用户输入文本文件
  • 读取文本文件
  • 拆分“,”字符上的内容
  • 查找以“A”或“A”开头的第一个字符串
  • 查找以“Z”或“Z”开头的第一个字符串
  • 如果是这样,这里有一种方法:

    // Prompt the user for a text file
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        // Read the text file
        string contents = File.ReadAllText(openFileDialog1.FileName);
    
        // Split the contents on the ',' character
        string[] items = contents.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries);
    
        // Create some variables to hold the first string that starts 
        // with 'A' and the first string that starts with 'Z'
        string firstAString = null;
        string firstZString = null;
    
        // Find the words and assign our variables
        foreach (string item in items)
        {
            // If the 'a' string hasn't been assigned yet and this item starts with 'a'
            if (firstAString == null && 
                item.StartsWith("A", StringComparison.OrdinalIgnoreCase))
            {
                firstAString = item;
            }
    
            // If the 'z' string hasn't been assigned yet and this item starts with 'z'
            else if (firstZString == null && 
                item.StartsWith("Z", StringComparison.OrdinalIgnoreCase))
            {
                firstZString = item;
            }
    
            // If both our strings have been assigned a new value, we can exit the loop
            if (firstAString != null && firstZString != null) break;
        }
    
        // Do something with the strings here
    }
    

    如果你花多一点时间来描述这个问题,那会很有帮助;整段文字并没有告诉我们任何有用的信息。1)您搜索的是文本(即
    lower
    ),而不是分隔的逗号数组(即
    split
    )。2) 您的
    while
    循环无效,因为由于
    中断
    ,它最多只能运行1次。我建议您使用
    foreach
    循环拆分,并使用
    Contains
    查找所需字符不在
    while
    循环中,循环中的任何内容都不会使
    while
    条件
    为false
    。另外,
    IndexOf
    如果
    count
    0
    (而且
    count
    start
    总是
    0
    ),则总是返回
    -1
    )…告诉你我是新生。哇,我搞砸了那么多次!!!这只是我的第二个节目。上次while循环是我尝试的最后一件事,我把它的n个比特切换了很多次…但是感谢所有的帖子,我会读它们,我会从头开始,因为我把它搞混了,没有感觉到我从微软那里得到的每个循环,是的,我对它进行了相当严重的篡改,但仍然,在我篡改之前它不起作用谢谢你的回复,我不想只是作弊和复制你的代码,所以我将尝试了解你在那里发生了什么…可能需要一两个小时,但总比作弊好。谢谢,伙计,我很感激为东西声明空值有什么用,没有空声明它还能工作吗?StartWith!!!哈哈嘿,我知道那个,但我确实不知道为什么我不能让它工作。可能是因为我没有使用&。我将它们作为
    null
    启动,这样在
    foreach
    循环中,我可以通过检查
    null
    来判断它们是否已被分配。此外,它还可以防止在
    foreach
    循环中找不到匹配项的情况,我会在之后尝试检查它们(变量不能使用,除非它们已被赋值)。在您能够用“不起作用”的确切内容更新问题之前,我认为没有人能提供帮助。不清楚您要做什么。创建一个表示字母表的字符串数组,然后使用foreach将每个需要比较的字符串表示为“a”,我可以通过使用字母表数组的索引作为比较选项来解决这个问题。
    //Loop to check dialog result 
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    //try&catch exception handler
    try
    //Block to try
    {
    //Create string of all text in file
    string someText = File.ReadAllText(openFileDialog1.FileName);
    //Converts someText to uppercase
    string upper = someText.ToUpper();
    //Shows the lowercase text
    char[] charSeparators = new char[] { ',' };
    string[] split = upper.Split(charSeparators,   StringSplitOptions.RemoveEmptyEntries);
    string b = "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";
    string[] A = b.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
    string c;
    string d;
    foreach (string a in split)
    {
    if (a.StartsWith(A[0]))
    {
    c = a;
    MessageBox.Show(c);
    }
    if (a.StartsWith(A[25]))
    {
    d = a;
    MessageBox.Show(d);
    break;
    }
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n" +
    $"Details:\n\n{ex.StackTrace}");
    }
    }
    }
    private void button2_Click(object sender, EventArgs e)
    {
    this.Close();
    }
    }
    }