C# 如何使用子字符串以及如何将一个变量分解为两个变量?
当我使用子字符串时,我一直在努力解决我的问题 C#:C# 如何使用子字符串以及如何将一个变量分解为两个变量?,c#,substring,C#,Substring,当我使用子字符串时,我一直在努力解决我的问题 C#: string sInput = "vegetable lasgane { receipt: cheese sauce etc...}"; 我想说的是: 使用子字符串删除“{”、“收据:”和“}” 然后将sInput分解为两个新变量,例如: 发件人: string sInput = "vegetable lasgane { receipt: cheese sauce etc...}"; string sMeal = "vegetable
string sInput = "vegetable lasgane { receipt: cheese sauce etc...}";
我想说的是:
- 使用子字符串删除“{”、“收据:”和“}”
- 然后将sInput分解为两个新变量,例如:
string sInput = "vegetable lasgane { receipt: cheese sauce etc...}";
string sMeal = "vegetable lasgane";
string sReceipt= "cheese sauce etc...";
至:
string sInput = "vegetable lasgane { receipt: cheese sauce etc...}";
string sMeal = "vegetable lasgane";
string sReceipt= "cheese sauce etc...";
如何在C#中执行此操作?您的代码示例将非常有用。谢谢 您可以执行Replace()
来删除字符,并执行Split(“{”)
来分隔两个字符串
string sInput = "vegetable lasgane { receipt: cheese sauce etc...}";
sInput = sInput.Replace("}", "");
sInput = sInput.Replace("receipt:", "");
string[] Parts = sInput.Split('{');
如果需要,现在可以Trim()
string sMeal = Parts[0].Trim();
string sReceipt= Parts[1];
就用这样的方法,
string sInput = "vegetable lasgane { receipt: cheese sauce etc...}";
string sMeal = sInput.Substring(0, sInput.IndexOf('{'));
Console.WriteLine(sMeal);
//vegetable lasgane
string sReceipt = sInput.Replace('{', ' ').Replace('}', ' ');
sReceipt = sReceipt.Substring(sReceipt.IndexOf(':') + 1).Trim();
Console.WriteLine(sReceipt);
//cheese sauce etc...
这里有一个。尝试使用value.split(“{”);
这里有更多的例子:这并不难:
sMeal = sInput.Split(" { receipt: ")[0];
及
输出:
餐:蔬菜拉斯甘
收据:奶酪酱等。
public void Foo()
{
int updationIndex = 0;
Func<string, char, string> getMyString1 = (givenString, takeTill) =>
{
var opString =
new string(
givenString.ToCharArray()
.TakeWhile(x => x != takeTill)
.ToArray());
updationIndex = inputString.IndexOf(givenString, StringComparison.CurrentCultureIgnoreCase)
+ opString.Length;
return opString;
};
var smeal = getMyString1(inputString, '{');
Console.WriteLine("Meal: " + smeal);
while (updationIndex < inputString.Length)
{
var sReceipt = getMyString(inputString.Remove(0, updationIndex), ':', '}');
Console.WriteLine("sReceipt: "+ sReceipt);
if (string.IsNullOrWhiteSpace(sReceipt))
{
break;
}
}
public void Foo()
{
int updateIndex=0;
Func getMyString1=(givenString,takeTill)=>
{
变量操作字符串=
新字符串(
givenString.ToCharArray()
.TakeWhile(x=>x!=takeTill)
.ToArray());
UpdateIndex=inputString.IndexOf(给定字符串、StringComparison.CurrentCultureIgnoreCase)
+opString.Length;
返回opString;
};
var smeal=getMyString1(inputString,{');
控制台写入线(“餐:+smeal”);
while(updateIndex
}
可能应该这样做。你试过正则表达式吗?@Downvoter是否愿意发表评论,至少让我看看我可能错在哪里?不,他只是想选出自己的选票……我也没有发表评论就得到了downvote。这里,让我+1告诉你,你的答案很好,你不应该在一周内得到downvotetroll@Downvoter:如果需要,请尝试添加一些注释你投了否决票,否则没人知道答案出了什么问题!这并不能解决问题,看起来很像我和索纳的答案。这怎么解决不了问题?他问了一个c#示例。我写这篇文章时没有注意到你的答案,抱歉,如果它冒犯了你,伙计。你的答案留下了“收据”他说需要放出来。你注意到最后的裂痕了吗?“收据”部分被省略了。
string sInput = "vegetable lasgane { receipt: cheese sauce etc...}";
string one = sInput.Substring(0, sInput.IndexOf("{"));
int start = sInput.IndexOf("{");
int end = sInput.IndexOf("}");
string two = sInput.Substring(start + 1, end - start - 1);
Response.Write(one + "\n"); // your 1st requirement
Response.Write(two + "\n"); // your 2nd requirement