A fragment of my.txt file looks like this:
18 Ladders/ladder_trigger (18 triggers) (18 enabled)
17 assets/prefabs/player/player_model.prefab/collision (0 triggers) (1 enabled)
17 assets/prefabs/misc/xmas/poweredlights/xmas.advanced.lights.deployed.prefab
(0 triggers) (17 enabled)
There are +1000 lines and their order is always random. My problem is that I'm trying to get an int which consists of the number of enabled values from a line which always contains text assets/prefabs/player/player_model.prefab/collision and output it in console. There is always only 1 line containing assets/prefabs/player/player_model.prefab/collision but the number of enabled values may differ and it's position in the text file is always different. I've tried to check a whole text file for this certain string and then try to figure it out from there on how to get that number but I have no idea on how to do that. Here's a part of my code which works fine but only for checking if the file contains that string or not but not for getting the number after that.
string[] words = File.ReadAllText(@"C:\Users\azir\Desktop\KelosRPR\Physics.Colliders.Objects.txt").Split(' ');
string wordtobesearched = "assets/prefabs/player/player_model.prefab/collision";
bool condition = false;
for (int i = 0; i < words.Length; i++)
{
if (words[i].Contains(wordtobesearched) == true)
{
condition = true;
break;
}
else
{
condition = false;
}
}
if (condition == true)
{
Console.WriteLine("{0} found in data", wordtobesearched);
}
else
{
Console.WriteLine("{0} not found in data", wordtobesearched);
}
You can use regex to find the enabled
number. Here's an example.
var text = File.ReadAllText(@"C:\Users\azir\Desktop\KelosRPR\Physics.Colliders.Objects.txt");
var wordtobesearched = "assets/prefabs/player/player_model.prefab/collision";
var regex = new Regex($"{wordtobesearched}.*?\\((\\d+) enabled\\)");
var number = int.Parse(regex.Match(text).Groups[1].Value);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.