INSTR函数浅析

2024-08-30 22:18
文章标签 函数 浅析 instr

本文主要是介绍INSTR函数浅析,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

--INSTR函数


{ INSTR| INSTRB| INSTRC| INSTR2| INSTR4}(string , substring [, position [, occurrence ] ])


The INSTR functions search string for substring. The search operation is defined as comparing the substring argument with substrings of string of the same length for equality until a match is found or there are no more substrings left. Each consecutive compared substring of string begins one character to the right (for forward searches) or one character to the left (for backward searches) from the first character of the previous compared substring. If a substring that is equal to substring is found, then the function returns an integer indicating the position of the first character of this substring. If no such substring is found, then the function returns zero.

返回找到的子串的位置。


position is an nonzero integer indicating the character of string where Oracle Database begins the search—that is, the position of the first character of the first substring to compare with substring. If position is negative, then Oracle counts backward from the end of string and then searches backward from the resulting position.

如果position值为负,则从右往左开始搜寻。


occurrence is an integer indicating which occurrence of substring in string Oracle should search for. The value of occurrence must be positive. If occurrence is greater than 1, then the database does not return on the first match but continues comparing consecutive substrings of string, as described above, until match number occurrence has been found.


Examples

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
--寻找CORPORATE FLOOR中从第三个字符开始第二次出现的'OR‘位置。


题目练习


http://www.itpub.net/thread-2072122-1-1.html


哪些选项实现了一个名为plch_between_2_4的函数,使得下列代码块执行之后会显示 "enXFeue" ?


BEGINsys.DBMS_OUTPUT.put_line (plch_between_2_4 ('StevenXFeuerstein', 'e'));
END;
/(A) 
CREATE OR REPLACE FUNCTION plch_between_2_4 (string_in   IN VARCHAR2, letter_in   IN VARCHAR2)RETURN VARCHAR2
IS
BEGINRETURN SUBSTR (string_in, INSTR (string_in, letter_in, 1, 2),   INSTR (string_in, letter_in, 1, 4)- INSTR (string_in, letter_in, 1, 2)+ 1);
END;
/(B) 
CREATE OR REPLACE FUNCTION plch_between_2_4 (string_in   IN VARCHAR2, letter_in   IN VARCHAR2)RETURN VARCHAR2
ISc_2nd   CONSTANT PLS_INTEGER:= INSTR (string_in, letter_in, 1, 2) ;c_4th   CONSTANT PLS_INTEGER:= INSTR (string_in, letter_in, 1, 4) ;
BEGINRETURN SUBSTR (string_in, c_2nd, c_4th - c_2nd + 1);
END;
/(C) 
CREATE OR REPLACE FUNCTION plch_between_2_4 (string_in   IN VARCHAR2, letter_in   IN VARCHAR2)RETURN VARCHAR2
ISl_2nd   PLS_INTEGER:= INSTR (string_in, letter_in, 1, 2);l_4th   PLS_INTEGER:= INSTR (string_in, letter_in, l_2nd + 1, 2);
BEGINRETURN SUBSTR (string_in, l_2nd, l_4th - l_2nd + 1);
END;
/(D) 
CREATE OR REPLACE FUNCTION plch_between_2_4 (string_in   IN VARCHAR2, letter_in   IN VARCHAR2)RETURN VARCHAR2
IS
BEGINRETURN SUBSTR (string_in, letter_in, 2, 4);
END;
/

A选项中相当于SUBSTR('StevenXFeuerstein', 5 , 11-5+1) 结果是enXFeue,BC选项其实和A选择表达的意思相同,D选项substr( string, start_position, [ length ] )参数超过了要求的所以报错,答案应该是ABC


这篇关于INSTR函数浅析的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1122086

相关文章

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

浅析Java中如何优雅地处理null值

《浅析Java中如何优雅地处理null值》这篇文章主要为大家详细介绍了如何结合Lambda表达式和Optional,让Java更优雅地处理null值,感兴趣的小伙伴可以跟随小编一起学习一下... 目录场景 1:不为 null 则执行场景 2:不为 null 则返回,为 null 则返回特定值或抛出异常场景

shell编程之函数与数组的使用详解

《shell编程之函数与数组的使用详解》:本文主要介绍shell编程之函数与数组的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录shell函数函数的用法俩个数求和系统资源监控并报警函数函数变量的作用范围函数的参数递归函数shell数组获取数组的长度读取某下的

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

C/C++错误信息处理的常见方法及函数

《C/C++错误信息处理的常见方法及函数》C/C++是两种广泛使用的编程语言,特别是在系统编程、嵌入式开发以及高性能计算领域,:本文主要介绍C/C++错误信息处理的常见方法及函数,文中通过代码介绍... 目录前言1. errno 和 perror()示例:2. strerror()示例:3. perror(

Kotlin 作用域函数apply、let、run、with、also使用指南

《Kotlin作用域函数apply、let、run、with、also使用指南》在Kotlin开发中,作用域函数(ScopeFunctions)是一组能让代码更简洁、更函数式的高阶函数,本文将... 目录一、引言:为什么需要作用域函数?二、作用域函China编程数详解1. apply:对象配置的 “流式构建器”最