C#
C#

help

Root Question Message

codingrookie
codingrookie12/19/2022
✅ how do i trim this string

i have this long string for e.g string word = "iahdsoihdaiiadi1124I ID:whatever sidaohiow12" ; is it possible to get only word that comes after Id: .
C#
Samarichitane
Samarichitane12/19/2022
        var word = "iahdsoihdaiiadi1124I ID:whatever sidaohiow12";
        
        var match = Regex.Match(word, @"ID:(.*)");
        
        var id = match.Groups[1].Value;
        
        Console.WriteLine(id);
AntonC
AntonC12/20/2022
another way would be
int idIndex = str.IndexOf("ID:");
int idStartIndex = idIndex + "ID:".Length;
int spaceIndex = str.IndexOf(" ", idStartIndex);
string id = str[idStartIndex .. spaceIndex];
codingrookie
codingrookie12/20/2022
whats supposed to be in them 2 dots
codingrookie
codingrookie12/20/2022
this got me every word that comes after id , i need only the word after id:
vdvman1
vdvman112/20/2022
Nothing goes "in them 2 dots", those 2 dots are actually part of the code, they are how you specify a range in C#
Specifically str[idStartIndex..spaceIndex] gives you the substring from idStartIndex (inclusive) to spaceIndex (exclusive)
HimmDawg
HimmDawg12/20/2022
Yeah, your regex is almost correct. .* will match everything after ID:. You could add a space after the capture group and then do .*? instead
HimmDawg
HimmDawg12/20/2022
Depending on the characters that can be in that id, you could also use ([\w\d]+) instead of (.*?). This way you don't need to match the space afterwards
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy