If you know that your string always follows this kind of structure, in particular the part of “display-name=ThePoinball;”, you can use regular expressions. They are a powerful tool to filter strings, however, if you are a beginner to coding, it might not be easy to wrap your head around them, as they can be challenging without any prior experience.
When learning to create regular expressions, websites and tools like Regexr greatly help in understanding and analysing them, as well as to find the one that you need.
This is parsing, and it’s an art form. Regular expressions as mentioned above are one solid way to parse, but I think that may be overcomplicating the problem for this particular string (if you don’t already understand regex).
For a string like this, you’ll want to split it into a number of tokens (which appear to be separated by ; characters), and then put that into a data structure you can use. This looks like each token could be put into a Dictionary, with the = sign being the divider between the key and the value for that dictionary. string.Split will be used heavily in this.
Roughly speaking, it might look like: (typed it in here, untested code, etc)
public Dictionary<string, string> ParseKVPs(string inputString, string tokenDivider, string equalsDivider) {
Dictionary<string, string> rtn = new Dictionary<string, string>();
string[] tokens = inputString.Split(tokenDivider[0]);
foreach (string token in tokens) {
string[] parts = token.Split(equalsDivider[0]);
if (parts.Length == 2) {
rtn.Add(parts[0], parts[1]);
}
else {
Debug.LogWarning($"Each token must have exactly one = sign; {token} is wrong.");
}
}
return rtn;
}
var myDict = ParseKVPs("some-key=some-value;x=5", ";", "=");
string val = myDict["some-key"];
string x = myDict["x"];
Debug.Log($"Value of some-key is {val}; value of x is {x}");
If you ONLY care about the display-name, you could probably also just use String.IndexOf to locate the substring “display-name=”, then use it again to find the first semicolon after that substring, and then grab whatever is between the two.
var splitPoint = message.IndexOf("display-name=", 1);
var splitPoint2 = message.IndexOf(";", splitPoint);
var chatName = message.Substring(splitPoint+12, splitPoint2-splitPoint-12);
Great. You might want to add some error-checking code to let you know what went wrong in case one of those strings is not found or something like that.
Also keep in mind that there might be weird edge cases where this gives the wrong answer, like if there was a badge named “display-name=” for some reason. Or maybe there’s no semicolon after the display-name if display-name happens to be the very last parameter in the string. In order to be sure that this is completely “correct”, you’d need to get the full technical specification for how the original string is generated and what it’s allowed to contain so that you could make sure you’ve covered every possible case. But based on the example you gave, I think you’re probably OK.
If you go the regex route, download Regex Coach. It lets you quickly prototype your regular expressions outside of your code so you don’t waste time rerunning your game figuring out why they aren’t working.