0

I have this text

"\This is a report 12000.csv\"

I want the text before the period, ie 12000 .

Is there a something similar to a SubString and CharIndex that's available in SQL Server? Or some other easy method?

Geezer
  • 513
  • 5
  • 17
  • 1
    Did you google "C# substring"? Your title says "text before specific character", what character? –  Jul 19 '19 at 14:51
  • 1
    Are you wanting a solution in C# or SQL? You have tagged as C# but ask for something similiar to `Substring` in SQL Server? – ivcubr Jul 19 '19 at 14:52
  • First search result for C# substring: https://learn.microsoft.com/en-us/dotnet/api/system.string.substring?view=netframework-4.8 – Thomas Weller Jul 19 '19 at 14:53
  • 2
    Yes there is something similar to substring and charindex. they are Substring and indexof – Dave Kelly Jul 19 '19 at 14:56

1 Answers1

2

This task achieved by using regular expressions. The code below will extract any number from provided string

var yourString = @"\This is a report 12000.csv\";
var foundNum = Regex.Match(yourString, @"\d+").Value;
Console.Write(foundNum);
//output 12000
var yourString1 = @"\This is a 8080 report .csv\";
var foundNum1 = Regex.Match(yourString1, @"\d+").Value;
Console.WriteLine(foundNum1);
//output 8080
Yuri
  • 2,820
  • 4
  • 28
  • 40