create or replace
function get_splitstr_loc
(
i_souString in varchar2,
i_desString in varchar2,
i_delimiter in varchar2
) return number
/*===============================
源字符串是多个小字符串由多分隔符连接而成的。
判断源字符串中是否有与目标字符串相等的小字符串。
=================================*/
is
v_count number(38, 0) := 0; --出现分割器的个数;
v_location1 number(38, 0) := 1; --出现前一个分割器的位置;
v_location2 number(38, 0) := 1; --出现前二个分割器的位置;
v_substr varchar2(4000) := ''; --两个分割器之前的字符串;
begin
loop
dbms_output.put_line('v_location1=' || v_location1);
v_location2 := instr(i_souString, i_delimiter, v_location1, 1);
--考虑最后一个字符的情况;
if(v_location2 = 0) then
v_substr := substr(i_souString, v_location1, length(i_souString) + 1 - v_location1);
else
v_substr := substr(i_souString, v_location1, v_location2 - v_location1);
--考虑多字符分割符号
v_location1 := v_location2 + length(i_delimiter);
end if;
v_count := v_count + 1;
dbms_output.put_line('v_location2=' || v_location2);
dbms_output.put_line('v_substr=' || v_substr);
dbms_output.put_line('v_count=' || v_count);
if(v_substr = i_desString) then
dbms_output.put_line('true');
exit;
else
dbms_output.put_line('false');
if(v_location2 = 0) then
v_count := 0;
exit;
end if;
end if;
end loop;
return v_count;
end get_splitstr_loc;
create or replace
function get_splitstr_str
(
i_souString in varchar2, --源字符串
i_location in varchar2, --第n个字符串
i_delimiter in varchar2 --分隔符
) return varchar2 --返回第n个字符串
/*===============================
=================================*/
is
v_count number(38, 0) := 0; --出现分割器的个数;
v_location1 number(38, 0) := 1; --出现前一个分割器的位置;
v_location2 number(38, 0) := 1; --出现前二个分割器的位置;
v_substr varchar2(4000) := ''; --两个分割器之前的字符串;
begin
loop
dbms_output.put_line('v_location1=' || v_location1);
v_location2 := instr(i_souString, i_delimiter, v_location1, 1);
--考虑最后一个字符的情况;
if(v_location2 = 0) then
v_substr := substr(i_souString, v_location1, length(i_souString) + 1 - v_location1);
else
v_substr := substr(i_souString, v_location1, v_location2 - v_location1);
--考虑多字符分割符号
v_location1 := v_location2 + length(i_delimiter);
end if;
v_count := v_count + 1;
dbms_output.put_line('v_location2=' || v_location2);
dbms_output.put_line('v_substr=' || v_substr);
dbms_output.put_line('v_count=' || v_count);
if(v_count = i_location) then
dbms_output.put_line('true');
exit;
else
dbms_output.put_line('false');
if(v_location2 = 0) then
v_substr := '';
exit;
end if;
end if;
end loop;
return v_substr;
end get_splitstr_str;
----测试用例1
I_SOUSTRING := '1,2,3,4,5,6';
I_LOCATION := 1;
I_DELIMITER := ',';
----测试用例2
I_SOUSTRING := '1,2,3,4,5,';
I_LOCATION := 6;
I_DELIMITER := ',';
----测试用例3
I_SOUSTRING := ',2,3,4,5,6';
I_LOCATION := 1;
I_DELIMITER := ',';
----测试用例4
I_SOUSTRING := '1,,2,3,4,5,6';
I_LOCATION := 2;
I_DELIMITER := ',';
----测试用例5
I_SOUSTRING := '1,,2,3,4,5,6';
I_LOCATION := 1;
I_DELIMITER := ',,';
----测试用例6
I_SOUSTRING := '1,,2,3,4,5,6';
I_LOCATION := 2;
I_DELIMITER := ',,';