文字列内の文字-の前にすべてを取得するための最良の方法を見つけようとしています。文字列の例を以下に示します。前の文字列の長さ-変化し、任意の長さにすることができます
223232-1.jpg
443-2.jpg
34443553-5.jpg
ですから、開始インデックス0から-の直前までの値が必要です。したがって、部分文字列は223232、443、および34443553になります。
文字列内の文字-の前にすべてを取得するための最良の方法を見つけようとしています。文字列の例を以下に示します。前の文字列の長さ-変化し、任意の長さにすることができます
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
string result = source.Substring(0, Math.Max(source.IndexOf('-'), 0))
s.Substring(0, n)を使用できます。s.Remove(n)sn
関数splitを使用します。
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();
}
文字列にがない場合は、文字列-全体を取得します。
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から始めて、ダッシュまでの(ダッシュを含まない)すべてのテキストを返します。
この目的で正規表現を使用できますが、入力文字列が正規表現と一致しない場合は、余分な例外を回避することをお勧めします。
まず、正規表現パターンにエスケープするという余分な頭痛を避けるために、その目的のために関数を使用することができます。
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());
}
}
ところで-正規表現パターンをに変更する"^(.*?)(-|$)"と、"-"パターンまで、またはパターンが見つからなかった場合にピックアップできます-文字列の最後まですべてをピックアップします。