简体   繁体   English

分割C#字串

[英]Split a C# string

I have build a program that converts DBF files and shows them in a Datagridview (C#). 我已经构建了一个转换DBF文件并在Datagridview(C#)中显示它们的程序。

It has a column in there that contains the initials & the appointment itself. 它在那里有一列,包含首字母和约会本身。

I also have made a program already that splits the initials and inserts them into the database. 我还制作了一个程序,该程序可以将缩写缩写并将其插入数据库。

Without going further into the program itself I would like to ask, how to I split the string? 在不深入探讨程序本身的情况下,我想问一下,如何分割字符串?

Example of data in a Cell; 单元中数据的示例; "WKO/JVM/RZO: Ingmar Boelens (GT)" “ WKO / JVM / RZO:Ingmar Boelens(GT)”

So every cell has data in it which contains the initials, followed by a ":" and then the appointment itself. 因此,每个单元格中都有包含首字母,后跟“:”和约会本身的数据。

The code that I am using right now; 我现在正在使用的代码;

string appointment;

            if (!string.IsNullOrWhiteSpace(row.Cells[2].Value.ToString()))
            {
                appointment = row.Cells[2].Value.ToString();
            }
            else
            {
                appointment = "No data available";
            }

            if (!string.IsNullOrWhiteSpace(appointment ))
            {
                appointment += " ";
                appointment = appointment.Substring(appointment.IndexOf(':') + 1, appointment.LastIndexOf(' '));
            }
            else
            {
                appointment = "No data available";
            }

So basically what I try is adding a white space at the end of every string, so that I can split them from the ":" to the last white space. 因此,基本上,我尝试在每个字符串的末尾添加一个空格,以便可以将它们从“:”拆分到最后一个空格。

However it doesn't work and I get an error saying "ArgumentOutOfRangeException". 但是,它不起作用,并且出现错误消息“ ArgumentOutOfRangeException”。 But not anything specific. 但没有什么具体的。

I know that where I try to split the second part of the string is not a legitimate condition, but how can I solve this? 我知道在哪里尝试分割字符串的第二部分不是合法条件,但是我该如何解决呢?

string s = "WKO/JVM/RZO: Ingmar Boelens (GT)";
string[] words = s.Split(':');
/*
*  words[0]= "WKO/JVM/RZO"
*  words[1] = "Ingmar Boelens (GT)";
*
*/

string[] data= = words[0].Split("/");

/*
*  data[0]= "WKO";
*  data[1] = "JVM";
*  data[2] = "RZO";
*/

The second argument of string.Substring() is Length, so the length FROM appointment.IndexOf(':') + 1 is being returned. string.Substring()的第二个参数是Length,因此将返回FROM约会的长度string.Substring() ':')+ 1。 You're getting the exception because the interval you are adding to the first argument exceeds the actual length of the string. 之所以会出现异常,是因为要添加到第一个参数的间隔超过了字符串的实际长度。 What you should do is subtract the first indexOf from the second, so: 您应该做的是从第二个索引中减去第一个indexOf,因此:

appointment = appointment.Substring(appointment.IndexOf(':') + 1, appointment.LastIndexOf(' ') - appointment.IndexOf(':'));

So basically what I try is adding a white space at the end of every string, so that I can split them from the ":" to the last white space. 因此,基本上,我尝试在每个字符串的末尾添加一个空格,以便可以将它们从“:”拆分到最后一个空格。

Oh. 哦。 You don't have to do that. 您不必这样做。 You can just do: 您可以这样做:

  appointment =  appointment.Substring(appointment.IndexOf(':')+1, appointment.Length - appointment.IndexOf(':')-1).Trim()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM