如何从Windows C中的文件中获取子字符串 [英] how to get the substring from file in windows c#

查看:59
本文介绍了如何从Windows C中的文件中获取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

有谁能告诉我如何从文件中获取前10个字符..

我的文本文件包含fisrt 10个字符作为用户名,另一个10个字符作为密码...,如下所示..

SUPERVISORM¾Óy‡
AJIT A ?? uX


我已经为此创建了代码,但无法正常工作...
请告诉我如何获取子字符串

我的代码在下面给出...


hi friends,

can any one tell me how can i get the first 10 charactor from the file ..

my text file contains the fisrt 10 char as user name and another 10char as password... which is given below..

SUPERVISORM¾Óy‡
AJIT AuX


i have create the code for that but not working well...
pls tell me how can i get the substring

my code is given below...


private void button1_Click(object sender, EventArgs e)
       {
           string[] str=File .ReadAllLines("E:\\sagar\\login_Demo\\USERS.USR") ;
           DataTable dt=new DataTable ();
           string[] temp = str[0].Split(',');
           foreach (string t in temp)
           {
               dt.Columns.Add (t, typeof(string));
           }
           for (int i = 1; i < str.Length; i++)
           {
               string[] t = str[i].Substring(0,10);
               dt.Rows.Add(t);
           }
           dataGridView1.DataSource = dt;
       }

推荐答案

发表评论后:

我知道.

这就是为什么我问您是否已阅读Substring的MSDN页面的原因.
然后,我指出Substring将返回string not 字符串数组(string []),就像您尝试做的那样.

我什至给了您两个如何正确执行操作的示例.

我会尝试更简单地解释它.

您不能使用:
After your comment:

I know.

This is why I asked if you had read the MSDN Page for Substring.

I then pointed out that Substring will return a string and not an array of strings (string[]) as you have tried to do.

I even gave you two examples of how to do it properly.

I''ll try to explain it more simply.

You cannot use:
string[] t = str[i].Substring(0,3);


string[]部分是错误的. string[]表示stringarray,它是>返回类型的 not .

阅读我链接到的页面.正确阅读.慢慢阅读.仔细阅读该页面上给出的示例,然后也许您会了解如何做.

使用从以上操作中获得的知识,对代码进行另一次尝试,然后再次询问您是否仍然被卡住.

我不会为您编写代码.

祝你好运. :)


It is the string[] part that is wrong. string[] indicates an array of string which is not the return type for Substring.

Read the page I linked to. Read it properly. Read it slowly. Work through the examples given on that page and then maybe you will understand how to do it.

Have another attempt at your code using the knowledge gained from doing the above and then ask again if you are still stuck.

I am not going to write your code for you.

Good luck. :)


string[] t = str[i].Substring(0,3);



该代码仅提取前4个字符.
您要10个对吗?



This code is only extracting the first 4 characters.
You want 10 right?


您的问题让我有些困惑.

您声明要使用子字符串获取前10个字符,而在代码中使用子字符串的唯一位置是

I am a little confused by your question.

You state that you want to get the first 10 characters using substring and yet the only place in your code where substring is used is

string[] t = str[i].Substring(0,3);



您是否已阅读 MSDN页面 [



Have you read the MSDN Page[^] for substring?

If you look at the definition, you will see that it returns a string not an array of strings.

So to get the first 10 characters you should use

string t = str[i].Substring(0, 10);


从位置0开始,获取10个字符.

如果密码使用以下10个字符


Start at position 0 and get 10 characters.

If the password is in the following 10 characters

string p = str[i].Substring(10, 10);


从位置10(第11个字符)开始,并获得10个字符.

希望这可以帮助! :)


Start at position 10 (the 11th character) and get 10 characters.

Hope this helps! :)


这篇关于如何从Windows C中的文件中获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆