Jump to content

# Getting first number from string of numbers.

You are about to reply to a thread that has been inactive for 4563 days.

Please take a moment to consider if this thread is worth bumping.

## Recommended Posts

I am working on a new project and am a little confused about the best way to handle a problem. I am trying to get the first number from a set of numbers of variable length. For example, I might have a set of numbers of 584739 and when entered into a function, it returns 5. Or I might have a set of numbers of 74832 and when entered into a function, it returns 7.

I can accomplish this mathematically but that would require about 50 lines, more than I really want to code if anyone has an easier way.

Thank you for your help :-)

##### Share on other sites

im no expert but this will work if your string is comma seperated..

`list data = llParseString2List(your_string), [","], []);             first_number = llList2String(data, 0);`

##### Share on other sites

I don't have a clear understanding of your problem. 584739 and 74832 are not sets of numbers, they are integers. If you want a function to return a first digit of an integer that would be:

integer GetFirstDigit(integer src)
{
if(src < 0) src=llAbs(src);
return (integer)(llGetSubString((string)src,0,0));
}

if however you missed commas and actually have lists [5,8,4,7,3,9] and [7,4,8,3,2] (which would be indeed sets of numbers), you don't need a function. There is a system call: llList2Integer(list src, integer list_index)

##### Share on other sites

Simply cast the integer into a string and get the first character: llGetSubString((string)i, 0, 0)

##### Share on other sites

Thanks a lot Ela Talaj and Darkie Minotaur, thats exactly what I needed :-)

##### Share on other sites

xx And you did it for free too!  Merry Christmas

##### Share on other sites

You are about to reply to a thread that has been inactive for 4563 days.

Please take a moment to consider if this thread is worth bumping.

## Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now
×
×
• Create New...