我怎样才能对ID进行子串?

时间:2012-03-29 16:57:18

标签: c# split substring

我得到了ID 这是 LSHOE-UCT 。我怎样才能将这些ID子串和分开才能成为:

gender = "L"
Product = "Shoe"
Category = "UCT"

这是我的代码:

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

    }

我需要分离它们,识别它们并从我的数据库插入差异表。这就是我遇到主要问题的地方

7 个答案:

答案 0 :(得分:1)

试试这个,如果有任何拼写错误,我只是随便打字道歉......

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;

答案 1 :(得分:1)

string[] s = AcStockCategoryID.Split('-');
string gender = s[0].Substring(0, 1);
string Product= s[0].Substring(1, s[0].Length - 1);
string Category = s[1];

答案 2 :(得分:1)

编辑:由于我的第一篇帖子中的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
}

答案 3 :(得分:1)

尝试不同的方法,这也可以。

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);

答案 4 :(得分: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

答案 5 :(得分:1)

我会倒退。

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;
}

然后只需将ID传递给LShoe_UctHandler ......

var lcu = LShoe_UctHandler("LGlobtrotters-TrainingShoes");
Console.WriteLine("gender = {0}", lcu.Gender);       
Console.WriteLine("Product = {0}", lcu.Product );         
Console.WriteLine("Category = {0}", lcu.Category );    

[手工键控 - 很抱歉打字错误和套管错误]

答案 6 :(得分:0)

试试这个:

        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]);