首页 > 试题广场 >

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

[编程题]查找字符串中逗号出现的次数
  • 热度指数:78402 时间限制: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
mysql中查找字符串中某个符号或字符出现的次数
例:查找字符串’10,A,B’ 中逗号’,'出现的次数?
用到的函数:
length(s)函数: s是字符串, 返回的是所求的字符串s的长度。
replace(a,b,c): 在字符串a中,将a中出现的b,替换成c。再把这个替换之后的串的结果返回。

题解答案:
select id,length(string)-length(replace(string,",","")) from strings;


发表于 2021-12-07 14:23:46 回复(5)
SELECT
    id,
    length(
        regexp_replace(STRING, '[A-Z0-9]', '')
    )
FROM
    strings;

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

发表于 2021-12-25 20:05:02 回复(1)
哦……原来是这个样子的呀
发表于 2021-12-08 10:30:05 回复(0)
select id, length(string)-length(replace(string,",",""))
from strings
发表于 2021-12-07 10:59:02 回复(0)
select length(string)-length(replace(string,',','') ) cont,id from strings;
这个的思路是,差值。
确定“,”出现的次数,现将“,”在字符串中都去掉,用原来的字符串的长度减去去掉后的字符串的长度,即可得出。
用到的知识点
1)length函数;
2)replace函数;replace(列名,被替换的值,替换值)
发表于 2022-02-03 20:25:40 回复(0)
select id,length(string)-length(replace(string,',','')) from strings ;
发表于 2023-05-25 16:43:42 回复(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;
此sql的意思就是,将string字段里的逗号,全部替换成空,每替换一个逗号,长度就会减一,那用起初的长度减去替换后的长度就是包含了几个逗号

发表于 2022-10-21 15:08:34 回复(0)
select id, length(string)-length(replace(string, ',','')) as cnt
from strings
发表于 2022-04-17 19:35:33 回复(0)
主要考察 字符串函数——length()函数,以及巧妙应用replace()函数去除逗号

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


发表于 2022-01-23 11:30:16 回复(1)
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)
SELECT
    id,(length(string)-length(replace(string,",",'')))AS cnt
FROM
    strings

发表于 2023-11-27 09:59:28 回复(0)
select id,length(string)-length(replace(string,',',''))
from strings

发表于 2023-10-31 20:57:08 回复(0)
select
    id,
    (
        length (string) - length (replace (string, ',', ''))
    ) as cnt
from
    strings
group by
    id;

发表于 2023-10-16 11:36:21 回复(0)
select id, count(%,) cnt
from strings

select id,length(string)-length(replace(string,',','')) cnt from strings

最开始思路是提取,从而计数,但是通配符%提取含 某字母的字符段,故不可提出 单个字母;
优化思路:总的字符串的长度减去去除逗号以后的字符串  得到的差值,即为逗号的长度
发表于 2023-10-12 14:58:24 回复(0)
select id,length(regexp_replace(string,'[0-9A-Z]','')) cnt
from strings
select id,length(string)-length(replace(string,',','')) cnt
from strings




发表于 2023-09-07 17:15:06 回复(0)
length(a) 计算a字段的字符串长度从1开始
replace(a,',','') 把a字段的,号变为空
所以相减就可以得出,号的数量

发表于 2023-07-28 14:13:32 回复(0)
select id,char_length(string)-char_length(replace(string,',',''))
from strings

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

发表于 2023-05-10 08:49:48 回复(0)