If you write a lot of PL/SQL code, sooner or later you will be faced with a requirement to count the number of occurrences of a substring in a string. In that case you may use this example to help yourself:
http://apex.oracle.com/pls/apex/f?p=31517:282
It will search not only for a single character withing a string but also for a substring of any length up to 4000 characters.
Why not use built-in functionality?
ReplyDeleteregexp_count('How manyline breaks arein thisstring?', '')
and of course the comment box remove the </br> tags...
ReplyDeleteAlex,
ReplyDelete1. it is easy if you know that function exists (I didn't think of regular expressions ;))
2. it is always good to know how to do it yourself :)
3. "and of course the comment box remove the tags..." is something I don't understand.
Denes
Denes,
ReplyDelete1. that does make it easier, yes :)
2. sure
3. I entered
regexp_count('How many</br>line breaks</br> are</br>in thisstring?', '</br>')
but the comment box removed the HTML tags... no matter
Alex
And if you're not on Oracle11 yet, when regexp_count was introduced, you can still use this:
ReplyDelete( length(p_string) -
length(replace(p_string,p_substring)
) / length(p_substring)
Regards,
Rob.
Sure, a cool way to find it out. Actually, the regexp function seems to be limited to 512 characters for the second parameter. This is a better way.
ReplyDeleteThanks,
Denes