Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, how can I split these strings as easily as possible:
var AL016 = "MS_FT_AL016_T";
var NF103 = "MS_FT_NF103_T";
var NF109 = "MS_FT_NF109_T";
var NF121 = "MS_FT_NF121_T";
var NF127 = "MS_FT_NF127_T";
var NF128 = "MS_FT_NF128_T";
var FTJmeno = "MS_FT_JMENO_P_T";
To make them look like this:
"AL016";
"NF103";
"NF109";
"NF121";
"NF127";
"NF128";
"JMENO_P";

Thank you so much

What I have tried:

I don't even know any function I could do.
Posted
Updated 17-Feb-22 12:15pm
Comments
PIEBALDconsult 17-Feb-22 9:00am    
Regular Expressions.

Use either the .NET Regular Expressions | Microsoft Docs[^] or the String.Split Method (System) | Microsoft Docs[^].

In future it would help if you explained exactly how you want to split them.
 
Share this answer
 
Comments
Maciej Los 17-Feb-22 14:24pm    
Very good advice!
dejf111 18-Feb-22 0:27am    
Thank you so much for the advice !!!
It really depends on how reliable that data is: if t6he prefix you want to ignore is always 6 characters, and the suffix is 2 then the simplest solution is just to use teh string.Substring function:
string inp = "MS_FT_AL016_T";
string outp = inp.Substring(6, inp.Length - (6 + 2));
 
Share this answer
 
Comments
Maciej Los 17-Feb-22 14:24pm    
Another very good advice!
dejf111 18-Feb-22 0:26am    
Thank you so much for the advice !!!
OriginalGriff 18-Feb-22 1:59am    
You're welcome!
Although (MS_FT_)([A-Z]+[0-9]*(_[A-Z])*)(_T) regex seems not very promising but it can give you a good start.

Try this

C#
public class Example
{
   using System;
   using System.Text.RegularExpressions;

   public static void Main()
   {

      string[] dataList = {"MS_FT_AL016_T", "MS_FT_NF103_T", "MS_FT_NF109_T", "MS_FT_NF121_T", "MS_FT_NF127_T", "MS_FT_NF128_T", "MS_FT_JMENO_P_T"};

      foreach (string data in dataList)
      {
         Console.WriteLine("Input string: " + data);
         try {
             string pattern = @"(MS_FT_)([A-Z]+[0-9]*(_[A-Z])*)(_T)";  
            Regex rg = new Regex(pattern);  
 
            Match m = rg.Match(data);
            Console.WriteLine("Captured string: " + m.Groups[2].Value);

         }
         catch (Exception) {
            Console.WriteLine("Not matched");
         }
         Console.WriteLine();
      }
   }
}
 
Share this answer
 
Comments
PIEBALDconsult 17-Feb-22 9:58am    
Don't instantiate the Regex object in the loop.
_Asif_ 18-Feb-22 0:39am    
true
Maciej Los 18-Feb-22 11:09am    
If you agree, please, improve your solution.
dejf111 18-Feb-22 0:27am    
Thank you so much for the advice !!!
Try this:

C#
string[] lines   = {"MS_FT_AL016_T", "MS_FT_NF103_T", "MS_FT_NF109_T", "MS_FT_NF121_T",
        "MS_FT_NF127_T", "MS_FT_NF128_T", "MS_FT_JMENO_P_T"};

string pattern = @"^(?>\w{2}_){2}(.*?)_";
Regex r = new Regex(pattern);
List<string> result = lines
    .Select(line => r.Matches(line)[0].Groups[1].Value)
    .ToList();
foreach(string s in result)
    Console.WriteLine(s);
 
Share this answer
 
Comments
dejf111 18-Feb-22 0:27am    
Thank you so much for the advice !!!
Maciej Los 18-Feb-22 11:08am    
You're very welcome. :)
_Asif_ 18-Feb-22 0:38am    
+5
Maciej Los 18-Feb-22 11:08am    
Thank you.
It looks like you're always getting rid of the text "MS_FT_" and "_T". If that's true, I'd keep it simple and use the Replace function. Here's how it works.

myString.Replace([Text/variable I'm looking for], [Text/variable to replace it with]);

So you could do something like this...
string[] stringList = { "MS_FT_AL016_T", "MS_FT_NF103_T", "MS_FT_NF109_T", "MS_FT_NF121_T", "MS_FT_NF127_T", "MS_FT_NF128_T", "MS_FT_JMENO_P_T" };

for (int i = 0; i < stringList.Length; i++)
{
    stringList[i] = stringList[i].Replace("MS_FT_", "").Replace("_T", "");
    Console.WriteLine(stringList[i]);
}

If you're comfortable with Regex, then go ahead with one of the solutions above, but it sounds like you're kinda new to this, so I recommend knowing how it works in order to maintain it. The replace function is about as simple as it gets.
 
Share this answer
 
v2
Comments
dejf111 18-Feb-22 0:27am    
Thank you so much for the advice !!!
PopeDarren 18-Feb-22 16:35pm    
You're very welcome!

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