删除字符串中的所有元音-PL/SQL
我有一个家庭作业:删除字符串中的所有元音-PL/SQL,sql,oracle,plsql,Sql,Oracle,Plsql,我有一个家庭作业: Write an anonymous PL/SQL block that accepts a string as input and removes all of the vowels (a.e.i.o.u) from the string, then outputs the results. The output should look like this: Run the Program SQL>@rm_vowels Enter the Strin
Write an anonymous PL/SQL block that accepts a string as input and removes all of the vowels (a.e.i.o.u) from the string, then outputs the results.
The output should look like this:
Run the Program
SQL>@rm_vowels
Enter the String: A penny for your thoughts
SQL>****************************
SQL>The new string is: pnny fr yr thghts
这看起来确实很容易做到,但我确实缺乏一些PL/SQL经验来完成这项工作
从我目前的搜索结果来看,我意识到我需要使用类似的东西:
REGEXP_REPLACE(name,'[a,e,i,o,u,A,E,I,O,U]','')
是吗?是的,此函数调用应该实现以下功能:
SELECT REGEXP_REPLACE('A penny for your thoughts','[a,e,i,o,u,A,E,I,O,U]','')
FROM dual;
您还可以使用translate函数,它可能比regexp\u replace稍微快一些:
select translate('A penny for your thoughts', 'xaeiouAEIOU', 'x') new_str from dual;
NEW_STR
------------------
pnny fr yr thghts
您可能希望进行修剪以删除任何前导/尾随空格。您可以使用
REGEXP\u REPLACE()
(尽管您当然不需要在字符类中使用逗号,而且您也不需要使用任何替换):
您还可以使用以下可能比使用正则表达式更有效的方法(也可以在Oracle 9i或更低版本中使用):
从技术上讲,分配调用匿名pl/sql块,并提示用户输入。所以你会有这样的东西:
set serveroutput on
set verify off
accept vstring prompt "Please enter your string: ";
declare
vnewstring varchar2(100);
begin
vnewstring := regexp_replace('&vstring', '[aeiouAEIOU]','');
dbms_output.put_line('The new string is: ' || vnewstring);
end;
/
您可以将其放入一个名为“my_Homegraphy_from_SO.sql”的文件中,并从该文件所在的同一目录登录到sqlplus并运行它:
@我的家庭作业来自_SO.sql
Please enter your string: This is a test
The new string is: Ths s tst
PL/SQL procedure successfully completed.
谢谢你的快速回复。很抱歉有任何愚蠢的问题,因为我是这方面的初学者,我现在正在努力学习,所以是的,我理解select regexp_替换,但是我们需要什么“FROM dual;”from?在这种情况下,我应该是插入字符串的人,而不是将其传递到我的函数中的人,对吗?@NetworkzZz-Oracle与其他数据库不同,它不支持没有
from
子句的select
s。因此,我们使用双虚拟表。请回答第二个问题-是-regexp\u replace
的第一个参数应该是要转换的列/值。为什么在字符类中使用逗号?如果这个问题涉及“Y”,那么它会更有趣。
set serveroutput on
set verify off
accept vstring prompt "Please enter your string: ";
declare
vnewstring varchar2(100);
begin
vnewstring := regexp_replace('&vstring', '[aeiouAEIOU]','');
dbms_output.put_line('The new string is: ' || vnewstring);
end;
/
Please enter your string: This is a test
The new string is: Ths s tst
PL/SQL procedure successfully completed.