How can I tweak the ID?

I got ID

that LSHOE-UCT . How can I tweak and split those ID

to become:

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

      

Here is my code:

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

    }

      

I need to split them, specify them and insert them into the diff table from my database. And this is where I have my main problem.

+3


source to share


7 replies


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

      



+1


source


try this, i just typed it randomly apologies if there are typos ...



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


source


EDIT: Updated my answer due to the wrong ID format in my first post.

If yours acStockCategoryID

will always be in a format LSHOE-UTC

, you can do something like the following:

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
}

      

+1


source


To try a different approach, this would work too.

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

      

+1


source


Warning : Overkill Completion

You can also use LINQ extension methods ( IEnumerable

) to accomplish this. I thought I would have a little thought experiment on how you could use the IEnumerable

solution to over-develop:

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

      

+1


source


I would do it back.

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

      

Then just pass the id to LShoe_UctHandler like so ...

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

      

[Manual key - sorry for typos and casing errors]

+1


source


Try the following:

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

      

0


source







All Articles