Click here to Skip to main content
15,922,166 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
string name = "S130A-1E734138G04_Trend";
char[] delimiters = new char[] {'-','_'};
            string[] parts = name.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
            Console.WriteLine("Lot Number")
                for (int i = 0; i < parts.Length; i++)
                {
                    Console.WriteLine(parts[i]);
                }



Hi! I want to write a code to extract out the characters "1E734138G04", which is after the "-" and before "_".

I have written these codes and I'm not sure how to extract out the characters that i want.

Thanks for any help!

What I have tried:

Wrote the codes above and some googling but I'm not sure which is the best method
Posted
Updated 4-Apr-18 22:03pm

Your part is in a middle:
C#
string result0 = parts[1];
Console.WriteLine(result0);


[EDIT]
Another ways:
C#
//linq methods: SkipWhile + TakeWhile
string result1 = string.Join("", name.SkipWhile(x=>x!=delimiters[0]).Skip(1).TakeWhile(x=>x!=delimiters[1]));
Console.WriteLine(result1);
//String.Substring
string result2 = name.Substring(name.IndexOf(delimiters[0])+1, name.IndexOf(delimiters[1]) - name.IndexOf(delimiters[0]) -1);
Console.WriteLine(result2);


For further details, please see:
Enumerable.SkipWhile Method (System.Linq)[^]
Enumerable.TakeWhile Method (System.Linq)[^]
String.Join Method (String, String[]) (System)[^]
String.Substring Method (Int32, Int32) (System)[^]
 
Share this answer
 
v2
Comments
CPallini 5-Apr-18 5:11am    
5.
Maciej Los 5-Apr-18 5:14am    
Thank you, Carlo.
Use regular expressions.
Try, for instance
C#
static void Main()
{
  string name = "S130A-1E734138G04_Trend";
  Regex regex = new Regex(@"-(\w+)_");
  Match match = regex.Match(name);
  if (match.Success)
  {
    if (match.Success)
    {
      string key = match.Groups[1].Value;
      Console.WriteLine(key);
    }
  }
}
 
Share this answer
 
Comments
Maciej Los 5-Apr-18 3:57am    
5ed!
CPallini 6-Apr-18 4:28am    
Thank you, man!
Member 13650651 6-Apr-18 4:15am    
Thank you CPallini! Worked perfectly. Appreciate your help!
CPallini 6-Apr-18 4:28am    
You are welcome.
Quote:
Hi! I want to write a code to extract out the characters "1E734138G04", which is after the "-" and before "_".
I have written these codes and I'm not sure how to extract out the characters that i want.

Isn't it what your code is doing ?
Your code gives you correct answer or not ?
How can you be unsure when you see the result on screen ?
Quote:
Wrote the codes above and some googling but I'm not sure which is the best method

For such an input, the difference between methods will be so tiny that you will not see a difference if you don't use it millions of time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900