• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Is there a SubString like function just for Integers?

Status
Not open for further replies.
Level 17
Joined
Apr 13, 2008
Messages
1,608
Is there a SubString-like function just for Integers?

Hi, I was looking for a method to get the xth digit of an integer but I couldn't find a solution.
Is there a way to do this with an already implemented function or something?

I created a tiny function for myself, but I don't want to litter my map with unnecessary stuff.

JASS:
function SubInt takes integer source, integer start, integer end returns integer
return S2I(SubString(I2S(source), start, end))
endfunction
 
Level 2
Joined
Jun 22, 2007
Messages
10
Converting to a substring then back again is a pretty inefficient way of doing it.

JASS:
function SubInt takes integer i, integer j, integer k returns integer
    set k = R2I(Pow(10, k-j))
    set i = i/R2I(Pow(10, j))
    return (i-(i/k)*k)
endfunction

JASS:
call BJDebugMsg(I2S(SubInt(123456789, 1, 5))) // displays "5678"
 
Status
Not open for further replies.
Top