user3554654
user3554654

Reputation: 33

Best way to get substrings of a longer string

What's the best way in c# to extract subtrings of a longer string like:

String str = "Car: volvo Wheels: 4 doors: 5";

Lets say I want the values volvo, 4, 5 inserted into

string car
string wheels
string doors

string newStr = str.Substring(read.IndexOf(':'), read.IndexOf(" "));

then after I have extracted the first String I'd remove that part

string str = strRemove(0, read.IndexOf(" ") + 1);

Then repeat this until everything was extracted. It's just tedious, and thought there would be a better way.

Upvotes: 1

Views: 298

Answers (4)

Nathan
Nathan

Reputation: 1317

How about...

string rawData = "Car: volvo Wheels: 4 doors: 5";
        var words = Regex.Split(rawData, @"\w+:").Select(x => x.Trim()).Where(x => x.Length > 0).ToList();
        var car = words[0];
        var wheels = words[1];
        var doors = words[2];

Upvotes: 1

Mike Hixson
Mike Hixson

Reputation: 5189

The most robust way to do this would be to use a regular expression. Others have provided regular expressions as solutions, but they do not cover the case where the car name has a space in it as in "Alfa Romeo". This solution would work better in those cases.

    string input = "Car: volvo Wheels: 4 doors: 5";

    Match match = Regex.Match(input, @"Car: (?<car>.+) Wheels: (?<wheels>\d+) doors: (?<doors>\d+)");

    Console.WriteLine(match.Groups["car"].Value);
    Console.WriteLine(match.Groups["wheels"].Value);
    Console.WriteLine(match.Groups["doors"].Value);

Upvotes: 0

Peter Duniho
Peter Duniho

Reputation: 70652

Lacking better detail, it's hard to know for sure what the right answer here is. But, assuming the labels "Car:", "Wheel:", and "doors:" are invariant, and you want to extract the variable values after each label, something like the following should accomplish your needs:

static readonly Regex _regex = new Regex(
    @"Car: (?<car>[^ ]*) Wheels: (?<wheels>[^ ]*) doors: (?<doors>[^ ]*)",
    RegexOptions.Compiled);

void SomeMethod(string text)
{
    Match match = _regex.Match(text);

    if (!match.Success)
    {
        return;
    }

    string car = match.Groups["car"].Value;
    string wheels = match.Groups["wheels"].Value;
    string doors = match.Groups["doors"].Value;
}

You can generalize the technique to any kind of string with a regular pattern of construction.

Upvotes: 1

jdweng
jdweng

Reputation: 34421

Try Regex

using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Car: volvo Wheels: 4 doors: 5";

            string pattern = @"Car:\s+(?'Car'[^\s]+)\s+Wheels:\s+(?'Wheels'[^\s]+)\s+doors:\s+(?'Doors'[^\s]+)";

            Match match = Regex.Match(input, pattern);
            string car = match.Groups["Car"].Value;
            string wheels = match.Groups["Wheels"].Value;
            string doors = match.Groups["Doors"].Value;
        }
    }
}
​

Upvotes: 1

Related Questions