WebI like this better than the chosen answer. (Ab)using GetFileName is semantically wrong since it's a directory you're trying to retrieve. Also, to make GetFileName deterministic means you have to account for the possibility of a trailing forward slash or backslash, and trim it off, which is ugly. – Webint index = str.IndexOf ('-'); string sub; if (index >= 0) { sub = str.Substring (0, index); } else { sub = ... // handle strings without the dash } Starting at position 0, return all text up to, but not including, the dash. Share Improve this answer Follow answered Dec 7, 2009 at 2:53 Michael Petrotta 59.6k 27 145 179
how to find string before a particular character in c#
WebOct 21, 2024 · In find_substrings () function call Substring () method to get the substrings. for (i = 1; i <= input_string.Length; i++) { for (j = 0; j <= input_string.Length - i; j++) { // Use Substring function Console.WriteLine (input_string.Substring (j, i)); } } Now show the retrieved substrings. Example: C# using System; class GFG { WebNov 9, 2024 · string text = "Retrieves a substring from this instance. The substring starts at a specified character position. Some other text"; string result = text.Substring(text.IndexOf('.') + 1,text.LastIndexOf('.')-text.IndexOf('.')) This will cut the part of string which lays … dissun\\u0027s mine how to get in
Substring in C# - C# Corner
WebOct 2, 2016 · In .NET, Regex support is in the System.Text.RegularExpressions library, so you'll have to reference that in your code. Here is a simple example: string pattern = "\$ ( [^\$]*)\$"; var matches = Regex.Matches (input, pattern); Share Improve this answer Follow edited Nov 26, 2013 at 10:27 answered Nov 26, 2013 at 10:18 Roy Dictus 32.3k 8 60 76 WebApr 12, 2024 · Substring Method in C#. The String class provides a method called Substring, which enables you to extract a portion of a string. ... In the following code … WebNov 29, 2024 · To get a mid string of a certain length, we call C#’s Substring () method on a string. We give that method two arguments. The first is the start index. The second is how many characters to return. That looks like: string.Substring (index, count). For example: string example = "Hello, World!"; dis study abroad it copenhagen