从字符串中删除所有元音 - PL/SQL
Removing all the vowels from a string - PL/SQL
我有这个作业:
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;
您还可以使用翻译功能,这可能比 regexp_replace:
稍微快一点
select translate('A penny for your thoughts', 'xaeiouAEIOU', 'x') new_str from dual;
NEW_STR
------------------
pnny fr yr thghts
您可能希望在周围加上 trim 以删除任何 leading/trailing 个空格。
您可以使用 REGEXP_REPLACE()
(尽管字符 class 中肯定不需要逗号 - 而且您不需要替换为任何内容):
SELECT REGEXP_REPLACE('A penny for your thoughts','[aeiouAEIOU]')
FROM dual;
您还可以使用以下方法,它可能比使用正则表达式更有效(并且也适用于 Oracle 9i 或更低版本):
SELECT TRANSLATE('A penny for your thoughts', 'AEIOUaeiou', ' ')
FROM dual
从技术上讲,作业需要匿名 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_homework_from_SO.sql" 的文件中,并从该文件所在的同一目录中登录到 sqlplus 并 运行 它:
@my_homework_from_SO.sql
Please enter your string: This is a test
The new string is: Ths s tst
PL/SQL procedure successfully completed.
我有这个作业:
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;
您还可以使用翻译功能,这可能比 regexp_replace:
稍微快一点select translate('A penny for your thoughts', 'xaeiouAEIOU', 'x') new_str from dual;
NEW_STR
------------------
pnny fr yr thghts
您可能希望在周围加上 trim 以删除任何 leading/trailing 个空格。
您可以使用 REGEXP_REPLACE()
(尽管字符 class 中肯定不需要逗号 - 而且您不需要替换为任何内容):
SELECT REGEXP_REPLACE('A penny for your thoughts','[aeiouAEIOU]')
FROM dual;
您还可以使用以下方法,它可能比使用正则表达式更有效(并且也适用于 Oracle 9i 或更低版本):
SELECT TRANSLATE('A penny for your thoughts', 'AEIOUaeiou', ' ')
FROM dual
从技术上讲,作业需要匿名 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_homework_from_SO.sql" 的文件中,并从该文件所在的同一目录中登录到 sqlplus 并 运行 它:
@my_homework_from_SO.sql
Please enter your string: This is a test
The new string is: Ths s tst
PL/SQL procedure successfully completed.