zoukankan      html  css  js  c++  java
  • Instr()函数的使用计算字符串中出现某个字母或单词的个数

    The following example searches the string "CORPORATE FLOOR", beginning with the third character, for the string "OR". It returns the position in CORPORATE FLOOR at which the second occurrence of "OR" begins:

    SELECT INSTR('CORPORATE FLOOR','OR', 3, 2)
      "Instring" FROM DUAL;
     
      Instring
    ----------
            14
    
    

    In the next example, Oracle counts backward from the last character to the third character from the end, which is the first "O" in "FLOOR". Oracle then searches backward for the second occurrence of OR, and finds that this second occurrence begins with the second character in the search string :

    SELECT INSTR('CORPORATE FLOOR','OR', -3, 2)
    "Reversed Instring"
         FROM DUAL;
     
    Reversed Instring
    -----------------
                   2
    
    默认查找‘o’的第一次出现的位置,默认从第一个字符开始:
    1 SELECT INSTR('Hello World','o') FROM dual;
    结果为:5
    默认查找‘o’的第一次出现的位置,指定从从第6个字符开始:
    1 SELECT INSTR('Hello World','o',6) FROM dual;
    结果为:8
    查找从第二次出现,从第一个位置开始的的'O'的位置:
    1 SELECT INSTR('Hello World', 'o', 1, 2) FROM DUAL;
    结果为:8
    使用INSTR计算字符出现的个数:
     1 DECLARE
    2 V_STRING_IN VARCHAR2(100) := 'Hello World';
    3 V_SUB_STR VARCHAR2(100) := 'o';
    4 V_COUNTS NUMBER;
    5
    6 FUNCTION GETCOUNT(P_STRING_IN VARCHAR2, P_SUB_STR VARCHAR2) RETURN NUMBER IS
    7 V_COUNT NUMBER := 1;
    8 V_SUBSTR_LOC NUMBER;
    9 BEGIN
    10 LOOP
    11 V_SUBSTR_LOC := INSTR(P_STRING_IN, P_SUB_STR, 1, V_COUNT);
    12 EXIT WHEN V_SUBSTR_LOC = 0 OR V_SUBSTR_LOC IS NULL;
    13 V_COUNT := V_COUNT + 1;
    14 END LOOP;
    15 RETURN V_COUNT - 1;
    16 EXCEPTION
    17 WHEN OTHERS THEN
    18 RETURN 'ERRORS';
    19 END;
    20
    21 BEGIN
    22 V_COUNTS := GETCOUNT(P_STRING_IN => V_STRING_IN, P_SUB_STR => V_SUB_STR);
    23 DBMS_OUTPUT.PUT_LINE('出现个数' || V_COUNTS);
    24 END;

    I believe that we are who we choose to be. Nobody‘s going to come and save you, you‘ve got to save yourself. 我相信我们成为怎样的人是我们自己的选择。没有人会来拯救你,你必须要自己拯救自己。
  • 相关阅读:
    836. Rectangle Overlap
    背包问题---01背包最优方案总数(原理剖析代码实现)
    背包问题---01背包(原理,伪代码,编程实现)
    DP---基本思想 具体实现 经典题目 POJ1160 POJ1037
    DP---(POJ1159 POJ1458 POJ1141)
    DP--HDU 1003(最大子串和)
    DP----入门的一些题目(POJ1088 POJ1163 POJ1050)
    DFS(DP)---POJ 1014(Dividing)
    博弈---斐波那契博弈
    元素相加交换另解&puts的一个用法
  • 原文地址:https://www.cnblogs.com/caroline/p/2319550.html
Copyright © 2011-2022 走看看