C
C#12mo ago
TheOneAndOnly

❔ What would be the best way to dynamicly cut in a string>

So for example I have this: "C:\Users\USERNAME\Documents\Paradox Interactive\Hearts of Iron IV\mod\HoI4-LotrMod\gfx\interface\goals\Generic\GFX_goal_generic_industry_I.tga" But Im only intrested in the location of the file within the folder "HoI4-LotrMod" altough that folder could in theory be called anything. What would be the best way to make sure I would always end up with the last "gfx\interface\goals\Generic\GFX_goal_generic_industry_I.tga" part
6 Replies
TheOneAndOnly
TheOneAndOnly12mo ago
Hope my qustion is clear this way 😅
Angius
Angius12mo ago
Split on "HoI4-LotrMod" and take the last part?
MODiX
MODiX12mo ago
Angius
REPL Result: Success
@"C:\Users\USERNAME\Documents\Paradox Interactive\Hearts of Iron IV\mod\HoI4-LotrMod\gfx\interface\goals\Generic\GFX_goal_generic_industry_I.tga".Split("HoI4-LotrMod")[^1]
@"C:\Users\USERNAME\Documents\Paradox Interactive\Hearts of Iron IV\mod\HoI4-LotrMod\gfx\interface\goals\Generic\GFX_goal_generic_industry_I.tga".Split("HoI4-LotrMod")[^1]
Result: string
\gfx\interface\goals\Generic\GFX_goal_generic_industry_I.tga
\gfx\interface\goals\Generic\GFX_goal_generic_industry_I.tga
Compile: 512.539ms | Execution: 38.347ms | React with ❌ to remove this embed.
mtreit
mtreit12mo ago
I would probably use IndexOf and Substring but that's arguably a matter of personal preference. I kind of have a personal dislike of the Split method because it often allocates a bunch of strings you don't care about. (Of course, now we're micro-optimizing.)
TheOneAndOnly
TheOneAndOnly12mo ago
Alright thanks
Accord
Accord12mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server