Resolved Using one string OR another

Bazza

Member
Joined
Nov 19, 2020
Messages
7
Programming Experience
Beginner
Hi Guys,

I have code that translates a text file so that it can be read into other software. However, the vendor file that I am translating, has changed two of the strings in the latest version (AD20). In the previous version (AD19) they used the strings Top Overlay and Bottom Overlay. In the new version (AD20) they use T.SilkS and B.SilkS to represent the same thing.

How should I change the code below so that it will read in both AD19 and AD20 versions?

C#:
private string LayerContent
{
    get
    {
        int indexStart = _line.IndexOf("NAME=Top Overlay"); //This works for AD19
        int indexEnd = _line.IndexOf("NAME=Bottom Overlay");   //This works for AD19
        //int indexStart = _line.IndexOf("NAME=T.SilkS");    //This works for AD20
        //int indexEnd = _line.IndexOf("NAME=B.SilkS");    //This works for AD20

        string context = _line.Substring(indexStart, indexEnd - indexStart);
        return context;
    }
}

Thanks for your help.
Bazza
 
Last edited by a moderator:
Solution
C#:
int indexStart = _line.IndexOf("NAME=Top Overlay");

if (index.Start == -1)
{
    indexStart = _line.IndexOf("NAME=T.SilkS");
}
Not that the String class has an IndexOfAny method but that only works for char values, not substrings. You could write your own extension method that worked for substrings, e.g.
C#:
public static class StringExtensions
{
    public static int IndexOfAny(this string source, params string[] anyOf)
    {
        var index = -1;

        foreach (var substring in anyOf)
        {
            index = source.IndexOf(substring);

            if (index != -1)
            {
                break;
            }
        }

        return index;
    }
}
Include that class in your project and you...
C#:
int indexStart = _line.IndexOf("NAME=Top Overlay");

if (index.Start == -1)
{
    indexStart = _line.IndexOf("NAME=T.SilkS");
}
Not that the String class has an IndexOfAny method but that only works for char values, not substrings. You could write your own extension method that worked for substrings, e.g.
C#:
public static class StringExtensions
{
    public static int IndexOfAny(this string source, params string[] anyOf)
    {
        var index = -1;

        foreach (var substring in anyOf)
        {
            index = source.IndexOf(substring);

            if (index != -1)
            {
                break;
            }
        }

        return index;
    }
}
Include that class in your project and you can then do this:
C#:
int indexStart = _line.IndexOfAny("NAME=Top Overlay", "NAME=T.SilkS");
In either case, you should put the most likely to be found first, so you avoid looking any further as often as possible. It's not the end of the world if you don't but it would be preferable.
 
Solution
Thank you for your input.

I tried your first suggestion but had to change index.Start to indexStart and index.End to indexEnd. However, I now get the Error - A local variable named 'indexEnd' cannot be declared in this scope because it would give a different meaning to 'indexEnd', which is already used in a 'parent or current' scope to denote something else
Cheers, Bazza
 
My apologies I had a typo in the text that I entered.
It now works beautifully - your assistance is much appreciated.
Cheers,
Bazza
 
Sorry about the rogue dot but, for the rest, that's on you. If you have this code currently working:
Old Code:
private string LayerContent
{
    get
    {
        int indexStart = _line.IndexOf("NAME=Top Overlay");
        int indexEnd = _line.IndexOf("NAME=Bottom Overlay");

        string context = _line.Substring(indexStart, indexEnd - indexStart);
        return context;
    }
}
then my suggestion will work if done properly:
New Code:
private string LayerContent
{
    get
    {
        int indexStart = _line.IndexOf("NAME=Top Overlay");

        if (indexStart == -1)
        {
            indexStart = _line.IndexOf("NAME=T.SilkS");
        }

        int indexEnd = _line.IndexOf("NAME=Bottom Overlay");

        if (indexEnd == -1)
        {
            indexEnd = _line.IndexOf("NAME=B.SilkS");
        }

        string context = _line.Substring(indexStart, indexEnd - indexStart);
        return context;
    }
}
 
Back
Top Bottom