简体   繁体   中英

The best way to split a string without a separator

I have string:

MONEY-ID123456:MONEY-STAT43:MONEY-PAYetr-1232832938

From the string above you can see that it is separated by colon ( : ), but in the actual environment, it does not have a standard layout.

The standard is the fields name, example MONEY-ID , and MONEY-STAT .

How I can I split it the right way? And get the value from after the fields name?

As Andre said, I would personally go with regular expressions. Use groups of something like,

"MONEY-ID(?<moneyid>.*)MONEY-STAT(?<moneystat>.*)MONEY-PAYetr(?<moneypay>.*)"

See this post for how to extract the groups. Probably followed by a private method that trims off illegal characters in the matched group (eg : or -).

Something like that should work:

string s = "MONEY-ID123456:MONEY-STAT43:MONEY-PAYetr-1232832938";
            Regex regex = new Regex(@"MONEY-ID(?<moneyId>.*?)\:MONEY-STAT(?<moneyStat>.*?)\:MONEY-PAYetr-(?<moneyPaetr>.*?)$"); Match match = regex.Match(s);       
            if (match.Success)
            {
                Console.WriteLine("Money ID: " + match.Groups["moneyId"].Value);


                Console.WriteLine("Money Stat: " + match.Groups["moneyStat"].Value);
                Console.WriteLine("Money Paetr: " + match.Groups["moneyPaetr"].Value);

            }

            Console.WriteLine("hit <enter>");
            Console.ReadLine();

UPDATE Answering additional question, if we're not sure in format, then something like the following could be used:

string s = "MONEY-ID123456:MONEY-STAT43:MONEY-PAYetr-1232832938";
        var itemsToExtract = new List<string> { "MONEY-STAT", "MONEY-PAYetr-", "MONEY-ID", };

        string regexFormat = @"{0}(?<{1}>[\d]*?)[^\w]";//sample - MONEY-ID(?<moneyId>.*?)\:
        foreach (var item in itemsToExtract)
        {
            string input = s + ":";// quick barbarian fix of lack of my knowledge of regex. Sorry
            var match = Regex.Match(input, string.Format(regexFormat, item, "match"));
            if (match.Success)
            {
                Console.WriteLine("Value of {0} is:{1}", item, match.Groups["match"]);
            }
        }

        Console.WriteLine("hit <enter>");
        Console.ReadLine();

Check this out:

string regex = @"^(?i:money-id)(?<moneyid>.*)(?i:money-stat)(?<moneystat>.*)(?i:money-pay)(?<moneypay>.*)$";
string input = "MONEY-ID123456:MONEY-STAT43:MONEY-PAYetr-1232832938";
Match regexMatch = Regex.Match(input, regex);

string moneyID = regexMatch.Groups["moneyid"].Captures[0].Value.Trim();
string moneyStat = regexMatch.Groups["moneystat"].Captures[0].Value.Trim();
string moneyPay = regexMatch.Groups["moneypay"].Captures[0].Value.Trim();

For example, using regular expressions ,

(?<=MONEY-ID)(\d)*

It will extract

123456

from your string.

Try

                    string data = "MONEY-ID123456:MONEY-STAT43:MONEY-PAYetr-1232832938";
                    data = data.Replace("MONEY-", ";");

                    string[] myArray = data.Split(';');
                    foreach (string s in myArray)
                    {
                        if (!string.IsNullOrEmpty(s))
                        {
                            if (s.StartsWith("ID"))
                            { 

                            }
                            else if (s.StartsWith("STAT"))
                            {

                            }
                            else if (s.StartsWith("PAYetr"))
                            { 

                            }
                        }
                    }

results in

ID123456:
STAT43:
PAYetr-1232832938

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM