- Home /
I want to add Dictionary of strings in game
I want to add Dictionary of about 200 strings in my game and want to compare input string given by user to the strings present in Dictionary i.e. whether input string has any meaning or not. Also i want to make sure that string in dictionary can be used only once i.e. repetition of strings is not allowed. Please Please help me with this. Thank You.
Answer by mchts · Mar 11, 2019 at 07:43 AM
Just use HashSet<string>
probably the most efficient collection for this job. It provides uniqueness i.e. it won't allow duplicate values. And whenever an input recieved you could check that with Contains() method.
Answer by toddisarockstar · Mar 20, 2019 at 04:15 AM
//drag and drop a txt file into this variable
// separate your ideas on seperate lines
public TextAsset textfile;
public string[] arr;
string lookForMatch (string word){
string r = "no matches found";
int i = arr.Length;
while (i>0) {i--;
if(arr[i]!=""&&arr[i].Contains(word)){
r = arr[i];
arr[i]="";break;}}
return r;
}
void Start () {
// seperates the text file at lines
arr = textfile.text.Split ("\n" [0]);
// will look for a word in you text file and give you the line only once
print ("I Found: "+lookForMatch ("bannana"));
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612193037im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Restricting input field to predefined words 1 Answer
Searching through a text file with Unity? 4 Answers
[JS] String,List dictionary doesn't compile. Any suggestions? 1 Answer
A new expression requires () or [] after type 1 Answer
Hashtable(or generic dictionary) vs Enum - when do I use which? 1 Answer