【问题标题】:How can I substring an ID?如何对 ID 进行子串化?
【发布时间】:2012-04-13 08:51:32
【问题描述】:

我有一个ID LSHOE-UCT。我怎样才能子串和分隔那些ID 以成为:

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

这是我的代码:

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

    }

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

【问题讨论】:

  • “L”代表什么?是否限制从某些固定的可能值中获取值?
  • @walther:如果是关于鞋子和其他相关产品,也许是“女士”?就像在女士部门与男士部门一样。只是猜测。
  • @David,是的,你可能是对的。在提出任何真正的解决方案之前,我只是想确定一下。

标签: c# split substring


【解决方案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】:

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

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

    【讨论】:

    • 您好,感谢您的评论。顺便说一句,我可以子字符串来获取 LSHOE 吗?
    【解决方案3】:

    试试这个,我只是随便打的,如果有错别字还请见谅……

    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;
    

    【讨论】:

      【解决方案4】:

      警告:完全矫枉过正

      您还可以使用 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】:

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

        【讨论】:

        • @Fuex - 我想通了,这就是为什么我的帖子中有EDIT: This answer is assuming all of your IDs are separated by -.。在您投反对票之前,我再次对其进行了编辑,以包含其他代码。
        • 对不起,重估答案:)。
        • 您好,感谢您的评论。顺便说一句,我可以子字符串来获取 LSHOE 吗?
        • @user998405 - 你不需要子字符串来获取 LSHOE。 Split('-') 为您提供 "LSHOE"splitParameter[0]"UTC"splitParameter[1]
        【解决方案6】:

        我会倒着做。

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

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

        【讨论】:

          【解决方案7】:

          试试这个:

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

          【讨论】:

            猜你喜欢
            • 2022-01-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-12-18
            • 2012-01-26
            • 1970-01-01
            相关资源
            最近更新 更多