Is there a way to parse strings better?

2019-04-03 01:21发布

I'm wondering if there's a built in way in .NET to parse bits of a string.

Take for example I have the following string:

"bsarbirthd0692"

made up of the following parts that will be cross referenced to data later:

Indexes   Purpose
0-3       (name)
4-9       (description)
10-13     (date mm-yy)

I'm hoping for something native like:

string name, desc, date;
string.ParseFormat("{0:4}{1:5}{2:4}", "bsarbirthd0692", out name, out desc, out date);

Is there a native way to do this in .NET or a popular library?

5条回答
祖国的老花朵
2楼-- · 2019-04-03 01:47

No, there's no built-in way. I would use string methods like Substring:

string name, desc, date;
if (input.Length >= 4)
{
    name = input.Substring(0, 4);
    if (input.Length >= 10)
    {
        desc = input.Substring(4, 6);
        if (input.Length >= 14)
        {
            date = input.Substring(10, 4);
        }
    }
}
查看更多
戒情不戒烟
3楼-- · 2019-04-03 01:48

You can use Regexp for this

string str= "bsarbirthd0692";
var regex = "(?<name>.{4})(?<desc>.{6})(?<date>.{4})";
MatchCollection matches = Regex.Matches(str, regex);
foreach(Match m in matches){
    Console.WriteLine(m.Groups["name"].ToString());
    Console.WriteLine(m.Groups["desc"].ToString());
    Console.WriteLine(m.Groups["date"].ToString());
}
查看更多
Evening l夕情丶
4楼-- · 2019-04-03 01:56

There is nothing like that, however writing something to implement:

IEnumerable<string> inputString.BreakIntoLengths(4, 6, 4)

with signature:

public IEnumerable<string> BreakIntoLengths(this string input, params int[] lengths);

is very easy:

public IEnumerable<string> BreakIntoLengths(this string input, params int[] lengths) {

  var pos = 0;
  foreach (var len in lengths) {
    yield return input.Substring(pos, len);
    pos += len;
  }
}

(With a real implementation having some error checking.)

NB. I've dropped the format string like interface: it appears to offer no value. Once the collection is returned it is easy to assign entries by index.

查看更多
干净又极端
5楼-- · 2019-04-03 02:05

Since a format is known, and shouldn't change Substring should work for you

string data = "bsarbirthd0692";
string name, desc, date;
name = data.Substring(0, 4);
desc = data.Substring(4, 6);
date = data.SubString(10);

EDIT

There's also extension methods you can create to do what ever you want. This is obviously more complex than previous suggestion

public static class StringExtension
{
    /// <summary>
    /// Returns a string array of the original string broken apart by the parameters
    /// </summary>
    /// <param name="str">The original string</param>
    /// <param name="obj">Integer array of how long each broken piece will be</param>
    /// <returns>A string array of the original string broken apart</returns>
    public static string[] ParseFormat(this string str, params int[] obj)
    {
        int startIndex = 0;
        string[] pieces = new string[obj.Length];
        for (int i = 0; i < obj.Length; i++)
        {
            if (startIndex + obj[i] < str.Length)
            {
                pieces[i] = str.Substring(startIndex, obj[i]);
                startIndex += obj[i];
            }
            else if (startIndex + obj[i] >= str.Length && startIndex < str.Length)
            {
                // Parse the remaining characters of the string
                pieces[i] = str.Substring(startIndex);
                startIndex += str.Length + startIndex;
            }

            // Remaining indexes, in pieces if they're are any, will be null
        }

        return pieces;
    }
}

Usage 1:

string d = "bsarbirthd0692";
string[] pieces = d.ParseFormat(4,6,4);

Result:

enter image description here

Usage 2:

string d = "bsarbirthd0692";
string[] pieces = d.ParseFormat(4,6,4,1,2,3);

Results:

enter image description here

查看更多
三岁会撩人
6楼-- · 2019-04-03 02:11

Better from what? Something like this?

var listStrings = new List<string>();
var tempIndex = 0;
var indexList = new List<int>{4, 6, 4}
foreach(var length in indexList)
{
    listStrings.Add(string.Substring(tempIndex , length);
    tempIndex += length 
}
查看更多
登录 后发表回答