-3

I am working on a project where i need to Iterate through every line in a txt file and find where its says .csproj and get the name of the csproj

C:\DataTypes\Nameofthecsproj.csproj this is what it looks like in txt file so i need to remove "C:\Datetypes\ until i only have: Nameofthecsproj.csproj

  foreach (string line in File.ReadAllLines(FILEPATH))
            {
                if (line.Contains(".csproj"))
                {
                    Match result = Regex.Match(line, @"^.*?(?=csproj)");

                }
            }

I just need a way to filter out the rest until i only have Nameofthecsproj.csproj

Jepsen_s
  • 39
  • 7
  • Take a look at: https://stackoverflow.com/questions/8224270/regular-expression-to-get-all-characters-before – James Gould Aug 04 '22 at 08:04
  • follow this: https://stackoverflow.com/questions/10709821/find-text-in-string-with-c-sharp – vksssd Aug 04 '22 at 08:05
  • You should use 'EndsWith()', use a regex as James Gould proposed or maybe String split (https://learn.microsoft.com/en-us/dotnet/csharp/how-to/parse-strings-using-split) – Kraego Aug 04 '22 at 08:07
  • The problem is, every new line the name of the csproj is different, i need to kinda loop backwards and get the name – Jepsen_s Aug 04 '22 at 08:21
  • Using ```Contains``` to check the file extensions is not a good idea, use [Path.GetExtension](https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getextension?view=net-6.0) function to check file extension – YHF Aug 04 '22 at 08:52
  • Does this answer your question? [Remove file extension from a file name string](https://stackoverflow.com/questions/7356205/remove-file-extension-from-a-file-name-string) – YHF Aug 05 '22 at 00:54

1 Answers1

1

refer : Find text in string with C# Use this method:

public static string getBetween(string strSource, string strStart, string strEnd)
{
    if (strSource.Contains(strStart) && strSource.Contains(strEnd))
    {
        int Start, End;
        Start = strSource.IndexOf(strStart, 0) + strStart.Length;
        End = strSource.IndexOf(strEnd, Start);
        return strSource.Substring(Start, End - Start);
    }
   return "";
}

How to use it:

string source = "This is an example string and my data is here";
string data = getBetween(source, "my", "is");
vksssd
  • 67
  • 7
  • You just use the code of other user: https://stackoverflow.com/questions/10709821/find-text-in-string-with-c-sharp. This is duplicated questions and answer. – euTIMER Aug 04 '22 at 08:17