[英]What's the best way to remove portions of a string? (C#)
I am looping through an array and there is a string in there that reads like this example: "1001--Some ingredient". 我循环遍历一个数组,其中有一个字符串,如下例所示:“1001 - 一些成分”。
Right now, as I loop through the array I am getting the whole string 现在,当我遍历数组时,我得到整个字符串
string ingCode = theData[i + 1];
But what I really need is simply "1001" and not the whole shibang. 但我真正需要的只是“1001”而不是整个shibang。
Combine some of the other methods listed in order to get the first portion or the code 结合列出的其他一些方法,以获得第一部分或代码
string myString = "1001--Some ingredient";
string myPortionOfString = myString.Substring(0, myString.IndexOf("--"));
This allows you to handle ingredient codes longer (or shorter) than 4 characters. 这使您可以处理比4个字符更长(或更短)的成分代码。
If the separator changes but the information you want is always a number, then use Regex to parse out the just the numbers. 如果分隔符发生更改但所需信息始终为数字,则使用正则表达式解析数字。
You can use SubString
method: 您可以使用
SubString
方法:
string myString = "1001--Some ingredient";
string myPortionOfString = myString.Substring(0, 4);
Console.WriteLine(myPortionOfString);
The console output is this: 控制台输出是这样的:
1001 1001
Did you refer to this? 你有没有提到这个?
EDIT: 编辑:
After seeing the comments, if you don´t know exactly how many numbers are before "--", the best answer is the one propossed by @Rob Allen . 看到评论之后,如果您不确切地知道“ - ”之前有多少数字,那么最好的答案就是@Rob Allen提出的答案。 I give him +1.
我给他+1。
//...
string myPortionOfString = myString.Substring(0, myString.IndexOf("--"));
//...
if the separator is always '--', you might give a shot to: 如果分隔符始终为“ - ”,您可以尝试:
string ingCode = theData[i+1].Split('-')[0];
If you're always interested in numbers in the beginning of the string, try a RegEx: 如果您始终对字符串开头的数字感兴趣,请尝试使用RegEx:
string ingCode = System.Text.RegularExpressions.Regex.Match(theData[i+1], @"^([0-9]*)").ToString();
You could use a regular expression: 您可以使用正则表达式:
Regex rgx = new Regex(@"(?<Code>\d+)--(?<Ingedient>\w+)", RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches("1001--Some Ingredient");
foreach(Match match in matches)
Console.WriteLine("Code:{0}, Ingredient:{1}",match.Groups["Code"], match.Groups["Ingredient"]);
Could be done in a few different ways, depending on what you know about what the data is going to look like. 可以通过几种不同的方式完成,具体取决于您对数据外观的了解。
Assumes we're always looking for the first 4 chars: 假设我们一直在寻找前4个字符:
string ingCode = theData[i + 1].Substring(0, 4);
Assumes we're looking for whatever comes before "--": 假设我们正在寻找“ - ”之前的任何内容:
string ingCode = theData[i + 1].Split(new string[] {"--"}, StringSplitOptions.None)[0];
Assumes we're looking for 1 or more digits at the start of the string: 假设我们在字符串的开头寻找一个或多个数字:
string ingCode = Regex.Match(theData[i + 1], @"^\d+").Captures[0];
You can use the StringBuilder class or simply create a new string by appending the indexes at [0], [1], [2], [3] (in the case that you always want the first 4 characters. You can also create a Left function: 你可以使用StringBuilder类,或者只是通过在[0],[1],[2],[3]处附加索引来创建一个新的字符串(在你总是需要前4个字符的情况下。你也可以创建一个左功能:
Console.Writeline(Left(myString, 4));
public static string Left(string param, int length)
{
string result = param.Substring(0, length);
return result;
}
Another thing you can do is create a string extension method: 您可以做的另一件事是创建一个字符串扩展方法:
static class StringExtensions
{
public static String Left(this string str, int numbOfChars)
{
if(str.Length <= numbOfChars) return str;
return str.Substring(0, numbOfChars);
}
public static String Right(this string str, int numbOfChars)
{
if numbOfChars >= str.Length) return str;
return str.Substring(str.Length, str.Length-numbOfChars);
}
}
You can call this like this: 您可以这样调用:
String test = "Hello World";
String str = test.Left(3); //returns Hel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.