skip to Main Content

I need a c# program which can split strings and copy some particular informations into another file.

I’ve a text file like this:

BRIDGE.V2014R6I1.SOFT
icem.V4R12I2.SOFT
mygale.V4R1I1.SOFT,patch01_MAJ_APL.exe
photoshop.V2014R10I1.SOFT
rhino.V5R0I1.SOFT,patch01_Update_Files.exe
TSFX.V2R3I2.SOFT,patch01_corrections.exe,patch02_clock.exe,patch03_correction_tri_date.exe,patch04_gestion_chemins_unc.exe

and I need only some of these information into another file as below :

BRIDGE,SOFT
ICEM,SOFT
MYGALE,SOFT
PHOTOSHOP,SOFT 

any helps pls 🙂

2

Answers


  1. I would split the string and create a new file with parts of the array you got from the split.

    You can split a string with eg. Split(".");

    And then e.g. create a new string stringname = splitstring[0] + "," + splitstring[2]

    That would add the first and third part back together.
    That would apply to your first line.

    Login or Signup to reply.
  2. As I don’t know, wether your text file is always like that, I can only provide a specific answer. First of all you have to, as ThatsEli pointed out, split the string at the point:

    var splitted = inputString.Split(".");
    

    Now it seems as though your second (zero based index) item has the irrelevant information with a comma splitted from the relevant. So all you have to do is to build together the zeroth and the second, while the second only has the first part before the comma:

    var res = $"{splitted[0]},{splitted[2].Split(",")[0]}";
    

    However, you seem to want your result in uppercase:

    var resUpper = res.ToUpper();
    

    But actually this only works as long as you have a perfect input file – otherwise you have to check, wether it actually has that many items or you’ll get an IndexOutOfRange exception.


    Actually I’m not sure wether you know how to read/write from/to a file, so I’ll provide examples on this as well.

    Read

    var path = @"YourPathToTheInputFile";
    if (!File.Exists(path))
    {
        Console.WriteLine("File doesn't exist! If you're using a console, otherwise use another way to print error messages");
        return;
    }
    var inputString = File.ReadAllText(path);
    

    Write

    var outputPath = @"yourOutputPath";
    if(!File.Exists(outputPath))
    {
        Console.WriteLine("You know what to put here");
        return;
    }
    File.WriteAllText(outputPath, inputString);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search