首页 > 试题广场 >

查找字符串中逗号出现的次数

[编程题]查找字符串中逗号出现的次数
  • 热度指数:79161 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
现有strings表如下:
  • id指序列号;
  • string列中存放的是字符串,且字符串中仅包含数字、字母和逗号类型的字符。
id string
1
2
3
10,A,B,C,D
A,B,C,D,E,F
A,11,B,C,D,E,G

请你统计每个字符串中逗号出现的次数cnt。
以上例子的输出结果如下:
id cnt
1
2
3
4
5
6

示例1

输入

drop table if exists strings;
CREATE TABLE strings(
   id int(5)  NOT NULL PRIMARY KEY,
   string  varchar(45) NOT NULL
 );
insert into strings values
(1, '10,A,B'),
(2, 'A,B,C,D'),
(3, 'A,11,B,C,D,E');

输出

1|2
2|3
3|5
select id,char_length(string)-char_length(replace(string,',','')) cnt from strings;

发表于 2024-02-06 10:56:20 回复(0)
select id,FLOOR((LENGTH(string)-1)/2) from strings;
发表于 2024-01-01 14:03:01 回复(0)
认为很多题解都不够严谨,首先length是计算占用空间,以字节为单位,该题没有明确字符集逗号所占空间不确定,所以应该要考虑','的长度.
select id, (length(string)- length(replace(string,',','')))/length(',') as cnt from strings;

发表于 2023-05-10 08:49:48 回复(0)
length(s)函数: s是字符串, 返回的是所求的字符串s的长度。
replace(a,b,c): 在字符串a中,将a中出现的b,替换成c。再把这个替换之后的串的结果返回。
‘10,A,B’ —> ‘10AB’
select id,length(string)-length(replace(string,',','')) cnt from strings

发表于 2023-02-23 17:45:10 回复(0)
select 
    id,
    length(string)-length(replace(string,",","")) as cnt
from 
    strings

-- 逗号个数 = 总长度 - 无逗号长度

发表于 2022-12-29 17:12:06 回复(0)
select id,(l1-l2) as cnt
from (
    select id,char_length(string) as l1,char_length(replace(string,',','')) as l2
    from strings
)t;

发表于 2022-12-24 15:10:30 回复(0)
select id, (length(string)-length(replace(string,',',''))) as cnt
from strings;
此sql的意思就是,将string字段里的逗号,全部替换成空,每替换一个逗号,长度就会减一,那用起初的长度减去替换后的长度就是包含了几个逗号

发表于 2022-10-21 15:08:34 回复(0)
字符函数复习
#length 获取参数值的字节个数
#concat 拼接字符串
# upper/ lower
# substr(ing) 截取字符串
注意:索引从1开始
select subtr('李莫愁爱上了陆展元', 7) out_put;
select substr('李莫愁爱上了陆展元', 1, 3) out_put;
# instr 返回子串第一次出现的索引,如果找不到返回0
# trim('要删除的字符' from '子串')
# lpad 用指定字符实现左填充指定长度
# rpad
# replace 替换



发表于 2022-09-23 16:11:08 回复(0)
select id,length(string) - length(replace(string,',','')) as cnt
from strings;

字符串总长度减去去掉逗号的字符串长度,就等于逗号的字符串长度,等同于逗号的出现次数
发表于 2022-06-02 17:28:04 回复(0)
select id, length(string) - length(replace(string,',','')) cnt
from strings

发表于 2022-04-18 12:38:40 回复(0)
select id, length(string)-length(replace(string, ',','')) as cnt
from strings
发表于 2022-04-17 19:35:33 回复(0)
select id,length(string)-length(replace(string,',',''))
from strings


发表于 2022-04-07 09:53:32 回复(0)
select id,length(string) - length(replace(string, ",", ""))  as cnt from strings;
# 解题思路:
# ①巧用length函数和replace,length函数计算字符串的长度,length("10,A,B")算出整个字符串的长度。
# ②使用replace将 , 替换为空,那么整个字符串减少的长度等于 , 的长度,两者相减就是 , 出现的次数。

发表于 2022-04-01 20:29:02 回复(0)
SELECT id,ROUND((LENGTH(string))/2) -1 cnt from strings 

发表于 2022-03-12 15:15:26 回复(0)
select id,
       length(string) - length(replace(string,",","")) as cnt
from strings

发表于 2022-03-12 15:07:13 回复(0)
主要考察 字符串函数——length()函数,以及巧妙应用replace()函数去除逗号

SELECT id, LENGTH(string) - LENGTH(REPLACE(string, ',', '')) cnt
FROM strings


发表于 2022-01-23 11:30:16 回复(1)
length和replace 一起使用
select 
id,LENGTH(string)-LENGTH(REPLACE(string,',','')) as cnt
from strings


发表于 2022-01-05 22:56:51 回复(0)
SELECT
    id,
    length(
        regexp_replace(STRING, '[A-Z0-9]', '')
    )
FROM
    strings;

使用正则表达是把字母数字删除。取长度即可

发表于 2021-12-25 20:05:02 回复(1)
select id,
       char_length(string)-char_length(replace(string,',','')) cnt
from strings

发表于 2021-12-18 23:19:16 回复(0)
为什么只用字符串”10,A,B“呢?也不用从strings表中筛选吗?
发表于 2021-12-07 15:36:07 回复(2)