1

当我使用子字符串时,我一直在努力解决我的问题。

C# :

string sInput = "vegetable lasgane { receipt: cheese sauce etc...}";

我想要什么:

  • 使用子字符串删除“{”、“receipt:”和“}”
  • 然后将 sInput 分解为新的两个变量,例如:

从 :

string sInput = "vegetable lasgane { receipt: cheese sauce etc...}";

至:

string sMeal = "vegetable lasgane";
string sReceipt= "cheese sauce etc...";

如何在 C# 中做到这一点?您的代码示例将非常受欢迎。谢谢!!

4

7 回答 7

2
string sInput = "vegetable lasgane { receipt: cheese sauce etc...}";
string[] splitString = sInput.Split('{');
string firstString = splitString[0];
string secondString = splitString[1].Replace("}", "").Split(':')[1];

应该可以解决问题。

于 2013-08-13T10:27:55.283 回答
1

只需使用String.SubStringString.IndexOf这样的方法;

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...

这里有一个DEMO

于 2013-08-13T10:22:10.927 回答
0

您可以Replace()删除字符并Split("{")分隔 2 个字符串

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];
于 2013-08-13T10:19:58.053 回答
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;
            }

        }

}

于 2013-08-13T10:24:07.197 回答
0

尝试value.split("{");

更多示例:http: //www.dotnetperls.com/split

于 2013-08-13T10:22:23.407 回答
0

这并不难:

sMeal = sInput.Split(" { receipt: ")[0];

sReceipt = sInput.Split(" { receipt: ")[1];
sReceipt = sReceipt.Substring(0,sReceipt.Length-1);
于 2013-08-13T10:22:38.440 回答
0
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
于 2013-08-13T10:32:23.197 回答