none
get a part of a string RRS feed

  • Question

  • Hello,

    I am developing in c# (inside a SSIS script task, so normal C#).

    I have a variable FileName which will assume a value like : "13_SECURITY_CODE_MAP_D_5011R.07_20171128.csv"

    I would like another variable to pick the value of just one part of this filename variable

    So basically I would like

    1)  To replace  this part (which is always the same)   "13_SECURITY_CODE_MAP_D_"  and I can do this with the built in replace function.

    2)   I want to get ride of all the staff after the dot , so in this case: ".07_20171128.csv"

    Goal is to keep just  in this new variable what is between "13_SECURITY_CODE_MAP_D_" and the "."

    Can you help me understand how can I do this? Can you provide me kind of a sample of code?

    Thank you 

     


    PR

    Friday, February 2, 2018 12:35 PM

All replies

  • There is a replace method

    x = x.Replace("OldStringToReplace", "Replacement");


    Success Cor

    Friday, February 2, 2018 1:02 PM
  • Hello,

    Try this

    var data = "13_SECURITY_CODE_MAP_D_5011R.07_20171128.csv";
    if (data.Contains("."))
    {
        var part1 = data.Split('.');
        var pos = part1[0].LastIndexOf("_");
        var result = $"{part1[0].Substring(0, pos - 1)}.";
        Console.WriteLine(result);
    }

    Result

    13_SECURITY_CODE_MAP_.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, February 2, 2018 1:30 PM
    Moderator
  • Try the following:
    var data = "13_SECURITY_CODE_MAP_D_5011R.07_20171128.csv";
    
    string part = data.Split('.')[0];
    string result = part.Substring(part.LastIndexOf('_') + 1);
    
    // result = "5011R"
    
    wizend
    Friday, February 2, 2018 4:14 PM
  • Try Regular Expressions too:

    string example = "13_SECURITY_CODE_MAP_D_5011R.07_20171128.csv";
    string result = Regex.Match( example, @"(?<=^13_SECURITY_CODE_MAP_D_)(.+)(?=[.].+[.]csv$)", RegexOptions.IgnoreCase ).Value;

    Designed for .csv files only.


    • Edited by Viorel_MVP Friday, February 2, 2018 8:40 PM
    • Proposed as answer by Fei HuModerator Wednesday, February 7, 2018 9:00 AM
    Friday, February 2, 2018 8:39 PM
  • And this is how I would do it:

    static string GetGoodPart(string From)
    {
        const string unwanted = "13_SECURITY_CODE_MAP_D_";
        if (!From.StartsWith(unwanted))
            return "";
        int x = From.IndexOf('.');
        if (x == -1)
            return "";
        return From.Substring(unwanted.Length, x-unwanted.Length);
    }

    That could be simplified but unlike most responses I am validating the data. If you add the data validation to the other responses then that adds more to them too.



    Sam Hobbs
    SimpleSamples.Info

    Friday, February 2, 2018 10:51 PM