C# 我如何子串一个ID?

C# 我如何子串一个ID?,c#,split,substring,C#,Split,Substring,我有一个ID 它们是LSHOE-UCT。我如何将那些ID子串和分隔开来,以便成为: gender = "L" Product = "Shoe" Category = "UCT" 这是我的密码: private void assignProductCategory(string AcStockCategoryID) { //What should I insert? string[] splitParameter = AcStockCategory

我有一个
ID
它们是LSHOE-UCT。我如何将那些
ID
子串和分隔开来,以便成为:

gender = "L"
Product = "Shoe"
Category = "UCT"
这是我的密码:

    private void assignProductCategory(string AcStockCategoryID)
    {
        //What should I insert?
        string[] splitParameter = AcStockCategoryID.Split('-');

    }

我需要将它们分开,标识它们,并从数据库中插入到差异表中。这就是我遇到的主要问题

试试这个,我只是随机输入,如果有任何输入错误,我道歉

string id = "LSHOE-UCT";    
string[] arr = id.Split('-');
string gender = id.Substring(0,1); // this will give you L
string product = arr[0].Substring(1); // this will give you shoe
string category = arr[1]; // this will give you UCT;
编辑:由于在我的第一篇文章中ID格式不正确,因此更新了我的答案。

如果您的
acStockCategoryID
始终采用
LSHOE-UTC
格式,则可以执行以下操作:

private void assignProductCategory(string AcStockCategoryID) 
{
    string[] splitParameter = AcStockCategoryID.Split('-');
    System.Text.StringBuilder sb = new System.Text.StringBuilder();

    sb.AppendLine("gender=" + splitParameter[0].Substring(0, 1));
    sb.AppendLine("Product=" + splitParameter[0].Substring(1));
    sb.AppendLine("Category=" + splitParameter[1]);

    // use sb.ToString() wherever you need the results
}

为了尝试不同的方法,这也会起作用

string id = "LSHOE-UCT";
string gender = id.Substring(0,1);

int indexOfDash = id.IndexOf("-");
string product = id.Substring(1, indexOfDash - 1);

string category = id.Substring(indexOfDash + 1);
试试这个:

        string id = "LSHOE-UCT";
        Console.WriteLine("Gender: {0}",id.Substring(0,1));
        Console.WriteLine("Product: {0}",id.Split('-')[0].Substring(1));
        Console.WriteLine("Product: {0}", id.Split('-')[1]);

警告:完全过杀

您还可以使用LINQ的扩展方法(
IEnumerable
)来实现这一点。我想我应该做一个小小的思考实验,看看如何使用
IEnumerable
过度设计解决方案:

int indexOfDash = id.IndexOf("-");

var firstPart = id.TakeWhile(s => s != '-');
var linqGender = firstPart.Take(1).ToArray()[0];  // string is L
var linqProduct = String.Join("", firstPart.Skip(1).Take(indexOfDash-1)); // string is SHOE

var secondPart = id.Skip(indexOfDash+1);
var linqCategory = String.Join("", secondPart);  //string is UCT

我会倒着做

public class LCU
{
    public string Gender {get; set;}
    public string Product {get; set;} 
    public string Category {get; set;}
    public LCU(){}
}

private static LSU LShoe_UctHandler(string id)
{
    var lcu = new LCU();
    var s = id.Split('-');
    if (s.length < 2) throw new ArgumentException("id");
    lcu.Category = s[1];
    lcu.Gender = s[0].Substring(0,1);
    lcu.Product = s[0].Substring(1);
    return lcu;
}

[手动键入-非常抱歉输入错误和大小写错误]

L代表什么?它是否仅限于从一些固定的可能值中获得一个值?@walther:如果是关于鞋子和其他相关产品,可能是“女士”?比如女装部和男装部。猜猜看。@David,是的,也许你是对的。在提出任何真正的解决方案之前,我只是想确定一下。@Fuex-我明白了,这就是为什么我要编辑
:这个答案是假设您的所有ID都被-。
分隔在我的帖子中。在你投否决票之前,我再次编辑了它,以包括其他代码。很抱歉,重新评估了答案:)。嗨,谢谢你的评论。顺便问一下,我可以使用子字符串来获取LSHOE吗?@user998405-您不需要使用子字符串来获取LSHOE。
Split('-')
让您使用
splitParameter[0]
使用
splitParameter[1]
“LSHOE”
。您好,感谢您的评论。顺便问一下,我可以通过子串获得LSHOE吗?
public class LCU
{
    public string Gender {get; set;}
    public string Product {get; set;} 
    public string Category {get; set;}
    public LCU(){}
}

private static LSU LShoe_UctHandler(string id)
{
    var lcu = new LCU();
    var s = id.Split('-');
    if (s.length < 2) throw new ArgumentException("id");
    lcu.Category = s[1];
    lcu.Gender = s[0].Substring(0,1);
    lcu.Product = s[0].Substring(1);
    return lcu;
}
var lcu = LShoe_UctHandler("LGlobtrotters-TrainingShoes");
Console.WriteLine("gender = {0}", lcu.Gender);       
Console.WriteLine("Product = {0}", lcu.Product );         
Console.WriteLine("Category = {0}", lcu.Category );