grady
grady

Reputation: 12785

Getting parts of a string and combine them in C#?

I have a string like this: C:\Projects\test\whatever\files\media\10\00\00\80\test.jpg

Now, what I want to do is to dynamically combine the last 4 numbers, in this case its 10000080 as result. My idea was ti split this and combine them in some way, is there an easier way? I cant rely on the array index, because the path can be longer or shorter as well.

Is there a nice way to do that?

Thanks :)

Upvotes: 3

Views: 144

Answers (8)

oberfreak
oberfreak

Reputation: 1807

to find the number you can use regex:

(([0-9]{2})\\){4}

use concat all inner Group ([0-9]{2}) to get your searched number.

This will always find your searched number in any position in the given string.

Sample Code:

static class TestClass {
    static void Main(string[] args) {

        string[] tests = { @"C:\Projects\test\whatever\files\media\10\00\00\80\test.jpg",
        @"C:\Projects\test\whatever\files\media\10\00\00\80\some\foldertest.jpg",
        @"C:\10\00\00\80\test.jpg",
        @"C:\10\00\00\80\test.jpg"};

        foreach (string test in tests) {
            int number = ExtractNumber(test);
            Console.WriteLine(number);
        }
        Console.ReadLine();
    }

    static int ExtractNumber(string path) {
        Match match = Regex.Match(path, @"(([0-9]{2})\\){4}");
        if (!match.Success) {
            throw new Exception("The string does not contain the defined Number");
        }

        //get second group that is where the number is
        Group @group = match.Groups[2];

        //now concat all captures
        StringBuilder builder = new StringBuilder();
        foreach (var capture in @group.Captures) {
            builder.Append(capture);
        }

        //pares it as string and off we go!
        return int.Parse(builder.ToString());
    }
}

Upvotes: 1

cederlof
cederlof

Reputation: 7383

var r = new Regex(@"[^\d+]");

var match = r
    .Split(@"C:\Projects\test\whatever\files\media\10\00\00\80\test.jpg")
    .Aggregate((i, j) => i + j);

return match.ToString();

Upvotes: 1

Gaijinhunter
Gaijinhunter

Reputation: 14685

A compact way using string.Join and Regex.Split.

string text = @"C:\Projects\test\whatever\files\media\10\00\00\80\test.jpg";
string newString = string.Join(null, Regex.Split(text, @"[^\d]"));    //10000080

Upvotes: 4

sll
sll

Reputation: 62544

Using LINQ:

string path = @"C:\Projects\test\whatever\files\media\10\00\00\80\test.jpg";
var parts = Path.GetDirectoryName(path).Split('\\');
string numbersPart = parts.Skip(parts.Count() - 4)
                          .Aggregate((acc, next) => acc + next);

Result: "10000080"

Upvotes: 1

vc 74
vc 74

Reputation: 38179

string input = "C:\Projects\test\whatever\files\media\10\00\00\80\test.jpg";
string[] parts = toSplit.Split(new char[] {'\\'});
IEnumerable<string> reversed = parts.Reverse();
IEnumerable<string> selected = reversed.Skip(1).Take(4).Reverse();
string result = string.Concat(selected);

The idea is to extract the parts, reverse them to keep only the last 4 (excluding the file name) and re reversing to rollback to the initial order, then concat.

Upvotes: 1

Ian
Ian

Reputation: 34509

Use String.Split

String toSplit = "C:\Projects\test\whatever\files\media\10\00\00\80\test.jpg";
String[] parts = toSplit.Split(new String[] { @"\" });

String result = String.Empty;
for (int i = 5, i > 1; i--)
{
   result += parts[parts.Length - i];
}

// Gives the result 10000080

Upvotes: 2

zmbq
zmbq

Reputation: 39023

If you always want to combine the last four numbers, split the string (use \ as the separator), start counting from the last part and take 4 numbers, or the 4 almost last parts.

If you want to take all the digits, just scan the string from start to finish and copy just the digits to a new string.

Upvotes: 1

Thomas
Thomas

Reputation: 595

You can rely on array index if the last part always is the filename. since the last part is always

array_name[array_name.length - 1]

the 4 parts before that can be found by

array_name[array_name.length - 2]

array_name[array_name.length - 3]

etc

Upvotes: 2

Related Questions