2016-04-16 12 views
1

plsqlで正しく実行されるプログラムを作成しましたが、出力が間違っています。論理的には、コードに関する問題は表示されません。plsqlで文章中の最後の単語から2番目の単語を取得する

begin 
    inpstr := rtrim(ltrim(regexp_replace(inp,'\s{2,}',''))); 
    lastpos := instr(inpstr,' ',-1); 
    out3 := SUBSTR(inpstr, INSTR(inpstr,' ',-1) + 1); 
    pos := instr(inpstr,' '); 
    out1 := substr(inpstr,1,pos); 
    out2 := substr(inpstr,pos,lastpos); 
end; 

O/P:ここではコードです

dbms_output.put_line('First Word:: '||out1||' '||'Second Word:: '||out2||' '||'Lastword:: '||out3); 

PROCEDURE SPLITWORDS compiled 
anonymous block completed 
First Word:: Welcome Second Word:: to the world of analyti Lastword:: analytics! 

しかし 'の世界に' 取得すべき2番目の単語が、それはanalytiをフェッチします。

誰も私のコードで何が間違っているか教えてもらえますか?

ありがとう、 Dex。

答えて

0

substr関数の3番目のパラメータは、末尾文字の位置ではなく部分文字列の長さです。 poslastpos(下記参照)から差し引くと、コードが動作します。

out2 := substr(inpstr, pos, lastpos - pos); 
+0

「analytiの世界へ」 as out2 – Dex

+0

@DexOops、申し訳ありません。私は私の答えを修正します –

0

あなたがout2を割り当てるときは、少なくともlastPosからposを減算する必要があります。

サンプルテキストのlastPos「アナリティクスの世界へようこそ!」は24になり、posは8を持っていますので、24の長さを持つPOS 8から部分は右...である:を私は最初と最後の単語間のすべての単語をしたい

関連する問題