Stripping a prefix

Started by Bruce Guenterover 25 years ago3 messagesgeneral
Jump to latest
#1Bruce Guenter
bruceg@em.ca

Greetings.

Is there a function available to strip a string from the start of a
string? trim and ltrim can strip any occurrence of a set of characters
from the start of a string, but I want to be able to strip an exact
string. In particular, if the string starts with "www." (including the
dot), then it should be stripped.

Thanks.
--
Bruce Guenter <bruceg@em.ca> http://em.ca/~bruceg/

#2Michael Blakeley
mike@blakeley.com
In reply to: Bruce Guenter (#1)
Re: Stripping a prefix

Date: Fri, 25 Aug 2000 09:35:43 -0600
From: Bruce Guenter <bruceg@em.ca>
To: pgsql-general@postgresql.org
Subject: Stripping a prefix

Is there a function available to strip a string from the start of a
string? trim and ltrim can strip any occurrence of a set of characters
from the start of a string, but I want to be able to strip an exact
string. In particular, if the string starts with "www." (including the
dot), then it should be stripped.

trim(leading 'www.' from foo)

-- Mike

#3Bruce Guenter
bruceg@em.ca
In reply to: Bruce Guenter (#1)
Re: Stripping a prefix

On Fri, Aug 25, 2000 at 09:28:59PM -0700, Ian Turner wrote:

If the string is like `^www\.', then trim `w' and `.'.

But this trims all 'w's and all '.'s, even if the string doesn't start
with 'www.':

select trim(leading 'www.' from 'wfoo');
ltrim
-------
foo
(1 row)

I ended up writing a function to do it, and for the data sets I'm
dealing with, it's fast enough:

CREATE FUNCTION strip_www (text ) RETURNS text AS
'BEGIN
IF position(''www.'' IN $1) = 1 THEN
RETURN substring($1 FROM 5);
ELSE
RETURN $1;
END IF;
END;' LANGUAGE 'plpgsql';
--
Bruce Guenter <bruceg@em.ca> http://em.ca/~bruceg/