105 lines
3.8 KiB
C#
105 lines
3.8 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace KssSmaPlaLib.Commons
|
||
{
|
||
public class StringUxer
|
||
{
|
||
public static string CreateACarInBikesString(
|
||
string bike,
|
||
string car,
|
||
string f1,
|
||
int count,
|
||
int luckyCount)
|
||
{
|
||
// 1. まず100個のバイクのリストを作成
|
||
List<string> list = Enumerable.Repeat(bike, count).ToList();
|
||
|
||
// 2. 0〜99のランダムな位置を1つ選ぶ
|
||
Random rand = new Random();
|
||
int randomIndex = rand.Next(0, count);
|
||
|
||
// 3. その位置を車に置き換える(ラッキー時はF1)
|
||
Random rand2 = new Random();
|
||
var lucky = rand2.Next(0, luckyCount) == 0;
|
||
list[randomIndex] = lucky ? f1 : car;
|
||
|
||
// 4. 文字列に結合
|
||
return string.Concat(list);
|
||
}
|
||
|
||
public static bool WildcardMatch(string input, string pattern)
|
||
{
|
||
if (string.IsNullOrEmpty(pattern)) return string.IsNullOrEmpty(input);
|
||
// "*" を ".*" に変換し、正規表現にする
|
||
string regexPattern = "^" + Regex.Escape(pattern).Replace("\\*", ".*") + "$";
|
||
return Regex.IsMatch(input, regexPattern, RegexOptions.IgnoreCase);
|
||
}
|
||
public static bool TryParseIniSignature(string iniValue, out string functionName, out List<string> args)
|
||
{
|
||
functionName = null;
|
||
args = null;
|
||
|
||
var pattern = @"^([a-zA-Z_][a-zA-Z0-9_]*)\((.*?)\)$";
|
||
var match = Regex.Match(iniValue.Trim(), pattern);
|
||
|
||
if (!match.Success)
|
||
return false;
|
||
|
||
functionName = match.Groups[1].Value;
|
||
var argsString = match.Groups[2].Value;
|
||
|
||
args = string.IsNullOrEmpty(argsString)
|
||
? new List<string>()
|
||
: argsString.Split(',').Select(arg => arg.Trim()).ToList();
|
||
|
||
return true;
|
||
}
|
||
|
||
public static bool TryExtractYearFromString(string str, out int year)
|
||
{
|
||
// var match = Regex.Match(str, @"\b(20\d{2})\b"); // 条件厳しめ
|
||
var match = Regex.Match(str, @"(20\d{2})"); // 条件緩め
|
||
year = match.Success ? int.Parse(match.Groups[1].Value) : -1;
|
||
return year != -1;
|
||
}
|
||
|
||
/// <summary>
|
||
/// CSVから文字列取得(インデックス指定:範囲外の時既定値)
|
||
/// </summary>
|
||
/// <param name="csv">CSV文字列(カンマ区切り)</param>
|
||
/// <param name="index">インデックス</param>
|
||
/// <param name="noString">範囲外の時の既定値</param>
|
||
/// <returns>取得した文字列</returns>
|
||
public static string GetStringByIndexFormCsv(string csv, int index, string noString = "(No name...)")
|
||
{
|
||
string[] strSplit = csv.Split(',');
|
||
if (index < 0 || index > strSplit.Length - 1) return noString;
|
||
return strSplit[index];
|
||
}
|
||
|
||
/// <summary>
|
||
/// 文字列中の改行コードのカット
|
||
/// </summary>
|
||
/// <param name="str"></param>
|
||
/// <returns></returns>
|
||
public static string CutKaigyo(string str)
|
||
{
|
||
return Regex.Replace(str,
|
||
// '\r'と'\n'とUnicode改行文字
|
||
// (\u2028 (行区切り文字)
|
||
// \u2029 (段落区切り文字)
|
||
// \u0085 (次行文字))
|
||
// の正規表現
|
||
@"\r\n|[\r\n\u2028\u2029\u0085]",
|
||
"",
|
||
// 複数回行う時のためにコンパイルしておく
|
||
RegexOptions.Compiled);
|
||
}
|
||
}
|
||
}
|