iNCEPTiON_
iNCEPTiON_

Reputation: 621

String formatting in C#?

I have some problems to format strings from a List<string>

Here's a picture of the List values:

pic1

Now I managed to manipulate some of the values but others not, here's what I used to manipulate:

string prepareStr(string itemToPrepare) {
    string first = string.Empty;
    string second = string.Empty;
    if (itemToPrepare.Contains("\"")) {
        first = itemToPrepare.Replace("\"", "");
    }
    if (first.Contains("-")) {
        int beginIndex = first.IndexOf("-");
        second = first.Remove(beginIndex, first.Length - beginIndex);
    }
    return second;
}

Here's a picture of the Result:

pic2

I need to get the clear Path without the (-startup , -minimzed , MSRun , double apostrophes).

What am I doing wrong here?

EDIT my updated code:

void getStartUpEntries() {

    var startEntries = StartUp.getStartUp();

    if (startEntries != null && startEntries.Count != 0) {

        for (int i = 0; i < startEntries.Count; i++) {

            var splitEntry = startEntries[i].Split(new string[] { "||" }, StringSplitOptions.None);

            var str = splitEntry[1];
            var match = Regex.Match(str, @"\|\|""(?<path>(?:\""|[^""])*)""");
            var finishedPath = match.Groups["path"].ToString();

            if (!string.IsNullOrEmpty(finishedPath)) {

                if (File.Exists(finishedPath) || Directory.Exists(finishedPath)) {

                    var _startUpObj = new StartUp(splitEntry[0], finishedPath,
                                                 "Aktiviert: ", new Uri("/Images/inWatch.avOK.png", UriKind.RelativeOrAbsolute),
                                                 StartUp.getIcon(finishedPath));

                    _startUpList.Add(_startUpObj);

                }
                else {

                    var _startUpObjNo = new StartUp(splitEntry[0], finishedPath,
                                           "Aktiviert: ", new Uri("/Images/inWatch.avOK.png", UriKind.RelativeOrAbsolute),
                                            StartUp.getIcon(string.Empty));

                    _startUpList.Add(_startUpObjNo);
                }
            }

            var _startUpObjLast = new StartUp(splitEntry[0], splitEntry[1],
                                         "Aktiviert: ", new Uri("/Images/inWatch.avOK.png", UriKind.RelativeOrAbsolute),
                                          StartUp.getIcon(string.Empty));

            _startUpList.Add(_startUpObjLast);

        }
        lstStartUp.ItemsSource = _startUpList.OrderBy(item => item.Name).ToList();
    }

Upvotes: 0

Views: 74

Answers (3)

knittl
knittl

Reputation: 265131

You could use a regex to extract the path:

var str = @"0Raptr||""C:\Program Files (x86)\Raptr\raptrstub.exe"" --startup"
var match = Regex.Match(str, @"\|\|""(?<path>(?:\""|[^""])*)""");
Console.WriteLine(match.Groups["path"]);

This will match any (even empty) text (either an escaped quote, or any character which is not a quote) between two quote characters preceeded by two pipe characters.

Upvotes: 1

Zein Makki
Zein Makki

Reputation: 30022

This is and update to your logic without using any Regex:

private string prepareStr(string itemToPrepare)
{
    string result = null;
    string startString = @"\""";
    string endString = @"\""";

    int startPoint = itemToPrepare.IndexOf(startString);

    if (startPoint >= 0)
    {
        startPoint = startPoint + startString.Length;

        int EndPoint = itemToPrepare.IndexOf(endString, startPoint);

        if (EndPoint >= 0)
        {
            result = itemToPrepare.Substring(startPoint, EndPoint - startPoint);
        }
    }

    return result;
}

Upvotes: 0

Whit Waldo
Whit Waldo

Reputation: 5197

Similarly, you could simply split on the double quotes as I see that's a repeating occurrence in your examples and take the second item in the split array:

var path = new Regex("\"").Split(s)[1];

Upvotes: 0

Related Questions