文字列の--文字の前にすべてを取得するための最良の方法を見つけようとしています。文字列の例を以下に示します。前の文字列の長さ-はさまざまで、任意の長さにすることができます
223232-1.jpg
443-2.jpg
34443553-5.jpg
したがって、開始インデックスの0から-の直前までの値が必要です。したがって、部分文字列は223232、443、および34443553になります。
class Program
{
static void Main(string[] args)
{
Console.WriteLine("223232-1.jpg".GetUntilOrEmpty());
Console.WriteLine("443-2.jpg".GetUntilOrEmpty());
Console.WriteLine("34443553-5.jpg".GetUntilOrEmpty());
Console.ReadKey();
}
}
static class Helper
{
public static string GetUntilOrEmpty(this string text, string stopAt = "-")
{
if (!String.IsNullOrWhiteSpace(text))
{
int charLocation = text.IndexOf(stopAt, StringComparison.Ordinal);
if (charLocation > 0)
{
return text.Substring(0, charLocation);
}
}
return String.Empty;
}
}
結果:
223232
443
34443553
344
34
分割機能を使用します。
static void Main(string[] args)
{
string s = "223232-1.jpg";
Console.WriteLine(s.Split('-')[0]);
s = "443-2.jpg";
Console.WriteLine(s.Split('-')[0]);
s = "34443553-5.jpg";
Console.WriteLine(s.Split('-')[0]);
Console.ReadKey();
}
文字列に a がない場合は、文字列-
全体が取得されます。
String str = "223232-1.jpg"
int index = str.IndexOf('-');
if(index > 0) {
return str.Substring(0, index)
}
このスレッドが開始されてから、事態は少し進行しました。
今、あなたは使うことができます
string.Concat(s.TakeWhile((c) => c != '-'));
これを行う1つの方法は、 :String.Substring
と一緒に使用することです。String.IndexOf
int index = str.IndexOf('-');
string sub;
if (index >= 0)
{
sub = str.Substring(0, index);
}
else
{
sub = ... // handle strings without the dash
}
位置0から開始して、ダッシュまでのすべてのテキストを返しますが、ダッシュは含まれません。
LINQy の方法
String.Concat( "223232-1.jpg".TakeWhile(c => c != '-') )
(ただし、null をテストする必要があります;)
この目的で正規表現を使用できますが、入力文字列が正規表現と一致しない場合に余分な例外が発生しないようにすることをお勧めします。
最初に、正規表現パターンへのエスケープの余分な頭痛を避けるために - その目的のために関数を使用することができます:
String reStrEnding = Regex.Escape("-");
これは何もしないことを知っています-「-」は と同じですがRegex.Escape("=") == "="
、たとえば文字が@"\"
.
次に、文字列の始まりから文字列の終わりまでを照合する必要があります。末尾が見つからない場合は、何も一致しません。(空文字列)
Regex re = new Regex("^(.*?)" + reStrEnding);
アプリケーションのパフォーマンスが重要な場合は、新しい Regex 用に別の行を作成し、そうでない場合は、すべてを 1 行にまとめることができます。
最後に文字列と照合し、一致したパターンを抽出します。
String matched = re.Match(str).Groups[1].ToString();
その後、別の回答で行われたように、別の関数を作成するか、インラインラムダ関数を作成できます。インライン ラムダ関数 (デフォルト パラメーターを許可しない) または個別の関数呼び出しの両方の表記法を使用して記述しました。
using System;
using System.Text.RegularExpressions;
static class Helper
{
public static string GetUntilOrEmpty(this string text, string stopAt = "-")
{
return new Regex("^(.*?)" + Regex.Escape(stopAt)).Match(text).Groups[1].Value;
}
}
class Program
{
static void Main(string[] args)
{
Regex re = new Regex("^(.*?)-");
Func<String, String> untilSlash = (s) => { return re.Match(s).Groups[1].ToString(); };
Console.WriteLine(untilSlash("223232-1.jpg"));
Console.WriteLine(untilSlash("443-2.jpg"));
Console.WriteLine(untilSlash("34443553-5.jpg"));
Console.WriteLine(untilSlash("noEnding(will result in empty string)"));
Console.WriteLine(untilSlash(""));
// Throws exception: Console.WriteLine(untilSlash(null));
Console.WriteLine("443-2.jpg".GetUntilOrEmpty());
}
}
ところで-正規表現パターンをに変更すると、パターン"^(.*?)(-|$)"
まで、"-"
またはパターンが見つからなかった場合-文字列の最後まですべてを取得できます。