How do I trim a string? 
Author Message
 How do I trim a string?

Hi, I'm trying to trim a string that contains both alphabetic and numeric
chars.  For example, "Call Detail For 012542916".

I want to remove all of the alphabetic (i.e. [a-zA-Z]) characters, so that
I'm just left with the numberic string ("012542916").

I've tried various functions - string matching, substitution and
translation.  I think I'll use the substr function, as below:

substr($wholestring, n);

where $wholestring = the complete string and n = the length of the
alphabetical part of the string.

So, I just need to know how to find the length of the alphabetical part of
the string.

Phew, that was long-winded!

Thanks,

Simon



Sat, 01 Dec 2001 03:00:00 GMT  
 How do I trim a string?

Quote:

> For example, "Call Detail For 012542916".
> I want to remove all of the alphabetic (i.e. [a-zA-Z]) characters, so that
> I'm just left with the numberic string ("012542916").

$string =~ tr/ a-zA-Z//d;

translate/space, alphabet/to nothing/ d=delete any thing without
matches.

Greg



Sat, 01 Dec 2001 03:00:00 GMT  
 How do I trim a string?
[Posted and a courtesy copy sent.]



Quote:
> Hi, I'm trying to trim a string that contains both alphabetic and numeric
> chars.  For example, "Call Detail For 012542916".

> I want to remove all of the alphabetic (i.e. [a-zA-Z]) characters, so that
> I'm just left with the numberic string ("012542916").

> I've tried various functions - string matching, substitution and
> translation.  I think I'll use the substr function, as below:

> substr($wholestring, n);

> where $wholestring = the complete string and n = the length of the
> alphabetical part of the string.

> So, I just need to know how to find the length of the alphabetical part of
> the string.

Are you sure you want to do it that way?  Here's how to do that in one
line:

$_ = 'Call Detail For 012542916';

substr($_, 0, length +(/(\D+)/)[0]) = "";

But any of the following is much nicer, and I'm sure there are Other
Ways To Do It also.

tr/0-9//cd;

s/\D+//;

s/.+ //;

$_ = (split)[-1];

--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/



Sat, 01 Dec 2001 03:00:00 GMT  
 How do I trim a string?
: Hi, I'm trying to trim a string that contains both alphabetic and numeric
: chars.  For example, "Call Detail For 012542916".

: I want to remove all of the alphabetic (i.e. [a-zA-Z]) characters, so that
: I'm just left with the numberic string ("012542916").

   After removing all of the alphabetic characters, you would
   be left with

      '   012542916'

   not

      '012542916'

   So I am not sure which you really want...

      tr/a-zA-Z//d;   # remove alphabetic characters

      tr/a-zA-Z //d;  # remove alphabetic and space characters

      tr/0-9//cd;     # remove non-digit characters

      s/.*?(\d+)$/$1/;  # remove all but digits at end of string

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Sat, 01 Dec 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Looking for a routine to trim white space (like VB trim$)

2. Looking for a routine to trim white space (like VB trim$)

3. Trim leading and trailing spaces from a string

4. trim string to (x) characters with sprintf()

5. trimming spaces from a string

6. How to trim a String

7. How do i trim a string in Perl??

8. NEED: Fast, Fast string trim()

9. How to trim strings?

10. Trim A String To 16 Characters

11. trimming strings in an array - newbie question

12. Doing a Clever String Thing

 

 
Powered by phpBB® Forum Software