文本函数:Left()和Right()、Insert()和Replace()、InStr()、Locate()和Position()、ELT()和FIELD()

89 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 7 天,点击查看活动详情

一、前言

大家好,今天文章的内容是几个文本函数的记录:

  • Left()Right()
  • Insert()Replace()
  • InStr()、Locate()Position()
  • ELT()FIELD()

二、内容

Left 和 Right

  • Left(s, n):返回串左边的字符,即返回字符串s的前n个字符。
mysql> SELECT Left("Hello", 3);
+------------------+
| Left("Hello", 3) |
+------------------+
| Hel              |
+------------------+
1 row in set (0.00 sec)

  • Right(s, n):返回串右边的字符,即返回字符串s的后n个字符。
mysql> SELECT Right("Hello", 3);
+-------------------+
| Right("Hello", 3) |
+-------------------+
| llo               |
+-------------------+
1 row in set (0.00 sec)

Insert 和 Replace

  • Insert(s1, x, n, s2):从字符串str中第pos个位置开始的len个字符替换为newstr
mysql> SELECT INSERT("Hello World", 1, 5, "aaaaa");
+--------------------------------------+
| INSERT("Hello World", 1, 5, "aaaaa") |
+--------------------------------------+
| aaaaa World                          |
+--------------------------------------+
1 row in set (0.00 sec)

  • Replace(s, s1, s2):将字符串 s 中的所有子串 s1 替换为 s2
mysql> select Replace('aaabbbccc', 'a', 'Aa');
+---------------------------------+
| Replace('aaabbbccc', 'a', 'Aa') |
+---------------------------------+
| AaAaAabbbccc                    |
+---------------------------------+
1 row in set (0.00 sec)

InStr、Locate 和 Position

  • InStr(str, substr):返回字符串 str 中第一次出现子串 substr 的位置。
mysql> select InStr("abcde", "c");
+---------------------+
| InStr("abcde", "c") |
+---------------------+
|                   3 |
+---------------------+
1 row in set (0.00 sec)
  • Locate(substr, str):获取子串 substr在字符串 str 中的开始位置。
mysql> select Locate("c", "abcde");
+----------------------+
| Locate("c", "abcde") |
+----------------------+
|                    3 |
+----------------------+
1 row in set (0.00 sec)
  • Locate(substr, str, pos):从主串 str 的第 pos 个位置开始,查找子串 substr在主串中的位置。
mysql> select Locate("c", "abcdabcd", 4);
+----------------------------+
| Locate("c", "abcdabcd", 4) |
+----------------------------+
|                          7 |
+----------------------------+
1 row in set (0.00 sec)
  • Position(substr in str):获取子串 substr在字符串 str 中的开始位置。
mysql> select Position("c" in "abcde");
+--------------------------+
| Position("c" in "abcde") |
+--------------------------+
|                        3 |
+--------------------------+
1 row in set (0.00 sec)

ELT 和 FIELD

  • ELT() 方法用于返回字符串列表的第 N 个元素,语法如下;
ELT(N, str1, str2, str3, ...)

如果 N = 1,那么返回字符串 str1,如果 N = 2,那么返回字符串 str2,以此类推。

举个例子:

mysql> SELECT Elt(3, 'Aa', 'Bb', 'Cc');
+--------------------------+
| Elt(3, 'Aa', 'Bb', 'Cc') |
+--------------------------+
| Cc                       |
+--------------------------+
1 row in set (0.00 sec)

  • FIELD() 函数用于查找指定字符串在字符串列表中的位置。

语法如下:

FIELD(str, str1, str2, str3,...)

查找字符串 str 在字符串列表 str1, str2, str3,... 中的位置。

举个例子:

mysql> SELECT FIELD('Cc', 'Aa', 'Bb', 'Cc');
+-------------------------------+
| FIELD('Cc', 'Aa', 'Bb', 'Cc') |
+-------------------------------+
|                             3 |
+-------------------------------+
1 row in set (0.00 sec)

三、后话

好了,今天的文章内容就到这里,感谢观看。