CSharp · Console
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("223232-1.jpg".GetUntil('-'));
Console.WriteLine("443-2.jpg".GetUntil('-'));
Console.WriteLine("34443553z-5.jpg".GetUntil('-'));
Console.ReadKey();
}
}
static class StringExtensions
{
/// <summary>
/// Get substring until first occurrence of given character has been found. Returns the whole string if character has not been found.
/// </summary>
public static string GetUntil(this string that, char @char)
{
return that[..(IndexOf() == -1 ? that.Length : IndexOf())];
int IndexOf() => that.IndexOf(@char);
}
}