PDA

View Full Version : Subtracting from substring


Tåke
11-25-2003, 02:23 PM
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.

GDC
11-25-2003, 04:08 PM
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, 04: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.

Tåke
11-27-2003, 01:50 PM
Thank you!!
Sorry for the late reply.

CGTalk Moderation
01-16-2006, 07: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.