如何通过字符串插值获取字符串的第一个字符?
How to get the first char of a string by string interpolation?
static void BuildStrings(List<string> sentences)
{
string name = "Tom";
foreach (var sentence in sentences)
Console.WriteLine(String.Format(sentence, name));
}
static void Main(string[] args)
{
List<string> sentences = new List<string>();
sentences.Add("Hallo {0}\n");
sentences.Add("{0[0]} is the first Letter of {0}\n");
BuildStrings(sentences);
Console.ReadLine();
}
//Expected:
//Hallo Tom
//T is the first Letter of Tom
但是我得到了:
System.FormatException: 'Input string was not in a correct format.'
如何在不改变BuildStrings
方法的情况下获取“Tom”的第一个字母?
您可以添加自定义格式并使用它代替 [0]。
看看这个:Custom format - maximum number of characters
你真的需要做这样的事情:
static void BuildStrings(List<string> sentences)
{
string name = "Tom";
foreach (var sentence in sentences)
Console.WriteLine(String.Format(sentence, name, name[0]));
}
static void Main(string[] args)
{
List<string> sentences = new List<string>();
sentences.Add("Hallo {0}\n");
sentences.Add("{1} is the first Letter of {0}\n");
BuildStrings(sentences);
Console.ReadLine();
}
这给了我:
Hallo Tom
T is the first Letter of Tom
这是一个奇怪的要求,没有内置的东西支持这个,但如果你真的必须这样做,你可以编写一个方法来解析索引器,如下所示:
public static string StringFormatExtended(string s, params object[] args)
{
string adjusted =
Regex.Replace(s, @"\{(\d+)\[(\d+)\]\}", delegate (Match m)
{
int argIndex = int.Parse(m.Groups[1].Value);
if (argIndex >= args.Length) throw new FormatException(/* Some message here */);
string arg = args[argIndex].ToString();
int charIndex = int.Parse(m.Groups[2].Value);
if (charIndex >= arg.Length) throw new FormatException(/* Some message here */);
return arg[charIndex].ToString();
});
return string.Format(adjusted, args);
}
用法:
static void BuildStrings(List<string> sentences, string name)
{
foreach (var sentence in sentences)
Console.WriteLine(StringFormatExtended(sentence, name));
}
static void Main(string[] args)
{
string name = "Tom";
List<string> sentences = new List<string>();
sentences.Add("Hello {0}\n");
sentences.Add("{0[0]} is the first Letter of {0}");
sentences.Add("{0[2]} is the third Letter of {0}");
BuildStrings(sentences, name);
Console.ReadLine();
}
输出:
Hello Tom
T is the first Letter of Tom
m is the third Letter of Tom
static void BuildStrings(List<string> sentences)
{
string name = "Tom";
foreach (var sentence in sentences)
Console.WriteLine(String.Format(sentence, name));
}
static void Main(string[] args)
{
List<string> sentences = new List<string>();
sentences.Add("Hallo {0}\n");
sentences.Add("{0[0]} is the first Letter of {0}\n");
BuildStrings(sentences);
Console.ReadLine();
}
//Expected:
//Hallo Tom
//T is the first Letter of Tom
但是我得到了:
System.FormatException: 'Input string was not in a correct format.'
如何在不改变BuildStrings
方法的情况下获取“Tom”的第一个字母?
您可以添加自定义格式并使用它代替 [0]。 看看这个:Custom format - maximum number of characters
你真的需要做这样的事情:
static void BuildStrings(List<string> sentences)
{
string name = "Tom";
foreach (var sentence in sentences)
Console.WriteLine(String.Format(sentence, name, name[0]));
}
static void Main(string[] args)
{
List<string> sentences = new List<string>();
sentences.Add("Hallo {0}\n");
sentences.Add("{1} is the first Letter of {0}\n");
BuildStrings(sentences);
Console.ReadLine();
}
这给了我:
Hallo Tom
T is the first Letter of Tom
这是一个奇怪的要求,没有内置的东西支持这个,但如果你真的必须这样做,你可以编写一个方法来解析索引器,如下所示:
public static string StringFormatExtended(string s, params object[] args)
{
string adjusted =
Regex.Replace(s, @"\{(\d+)\[(\d+)\]\}", delegate (Match m)
{
int argIndex = int.Parse(m.Groups[1].Value);
if (argIndex >= args.Length) throw new FormatException(/* Some message here */);
string arg = args[argIndex].ToString();
int charIndex = int.Parse(m.Groups[2].Value);
if (charIndex >= arg.Length) throw new FormatException(/* Some message here */);
return arg[charIndex].ToString();
});
return string.Format(adjusted, args);
}
用法:
static void BuildStrings(List<string> sentences, string name)
{
foreach (var sentence in sentences)
Console.WriteLine(StringFormatExtended(sentence, name));
}
static void Main(string[] args)
{
string name = "Tom";
List<string> sentences = new List<string>();
sentences.Add("Hello {0}\n");
sentences.Add("{0[0]} is the first Letter of {0}");
sentences.Add("{0[2]} is the third Letter of {0}");
BuildStrings(sentences, name);
Console.ReadLine();
}
输出:
Hello Tom
T is the first Letter of Tom
m is the third Letter of Tom