View Full Version : Subtracting from substring
string $getItem = `ls -sl -l -tl 1`;
Now I want to delete the last two characters of the objects name if the characters are numeric, is it possible?
I can't seem to find any numericsearch or a way of deleting numbers when it's mixed with text.
This script is supose to select objects of nearly the same name with the click of a button.
|
|
Something like this should do what you want.
string $getItem = `ls -sl -l -tl 1`;
string $noNumberString;
for($i=0;$i<size($getItem);$i++)
{
string $hasNum=`match ("[0-9][0-9]$") $getItem[$i]`;
if($hasNum != ""){
$noNumberString=`substring $getItem[$i] 1 (size($getItem[$i])-2)`;
}
I haven't fully tested this, but it should give you some ideas for doing it.
--g
Jhavna
11-25-2003, 03:11 PM
one way to do this, is to simply use `match`
$no_numbers = match "[^0-9]+" $stringwithnumbers;
the no_numbers string will hold the string without the numbers in. (of course, that will strip ALL numbers from the string.
EDIT(2):
To rework GDCs example to use any number of numbers at the end of the string you can:
string $getItem = `ls -sl -l -tl 1`;
string $noNumberString;
for($i=0;$i<size($getItem);$i++)
{
string $hasNum=`match ("[0-9]+$") $getItem[$i]`;
int $digits = `size ($hasNum)`;
if($hasNum != "")
{
$noNumberString=`substring $getItem[$i] 1 (size($getItem[$i])-$digits)`;
}
Or something to that effect.... you get the principal logic behind it though
That way you can have any number of numbers at the end. As $hasNum holds the stripped numbers, size will give you the number of digits.
Thank you!!
Sorry for the late reply.
CGTalk Moderation
01-16-2006, 06:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2013, Jelsoft Enterprises Ltd.