Can you search for just the keys that start with a certain string?

Discussion and help for Easy Save 3
Post Reply
Recktardeded
Posts: 24
Joined: Sun Jun 12, 2022 8:30 am

Can you search for just the keys that start with a certain string?

Post by Recktardeded »

Hello, i would like to have multiple types of object types, and the key starts with "Type"(that can be type1, type2,etc) then a "/" to separate the words and then continues with one or more properties "size1,size2" and so on, so i save those in a file, that s why i use something like:
foreach (string key in ES3.GetKeys("ObjectTypes.es3"))
{
string typeString = "a function that i use to get just the type of object-type1 or type2 etc"
if (!TypesList.Contains(typeString))
{
TypesList.Add(typeString);
}
}
This works fine, but it loads for example 5 objects of type1 and 7 of type3, and i would like to reuse the strings, is there a way to load only the keys that start with type1? if not, then i ll just save all the keys when i call the function mentioned above and i ll filter it in another way. Thank you for your time, have a great day!
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Can you search for just the keys that start with a certain string?

Post by Joel »

Hi there,

As this would be a general coding question (i.e. searching a List for an string containing a substring) you might be better off asking on StackExchange or the Unity forums. However, generally if you want to get all of the keys from a file which begin with a certain substring then you could do something like the following:

Code: Select all

public List<string> GetKeysBeginningWith(string substring)
{
    return ES3.GetKeys().Where(s => s.StartsWith(substring)).ToList();
}
So if you had a file which contains the keys Type1_MyKey, Type2_MyKey, Type3_MyKey, Type1_MyKey2, Type1_MyKey3, Type4_MyKey, Type5_MyKey, the following method call would return a List containing Type1_MyKey, Type1_MyKey2, Type1_MyKey3:

Code: Select all

List<string> filtered = GetKeysBeginningWith("Type1");
All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply