指点成金-最美分享吧

登录

string怎么判断字符串是不是包含某几个字符

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了string怎么判断字符串是不是包含某几个字符相关的知识,希望对你有一定的参考价值。

  判断一个字符串是否包含某几个字符方法如下:

  参考代码:

  

  输出结果

  

  关键代码:isContain方法

    描述

    java.lang.String.contains() 方法返回true,当且仅当此字符串包含指定的char值序列

    声明

    以下是声明java.lang.String.contains()方法

    public boolean contains(CharSequence s)

    参数

    s -- This is the sequence to search for.

    返回值

    此方法返回true,如果此字符串包含,否则返回false。

参考技术A 有两种方法可以使用,C++风格,或C风格。
一、C++风格。
C++的string类提供了字符串中查找另一个字符串的函数find。
其重载形式为:
string::size_type
string::find(string
&);
功能为在string对象中,查找参数string类型的字符串是否存在,如果存在,返回起始位置。不存在则返回
string::npos。
参考代码如下:
#include
<iostream>
#include

using
namespace
std;
int
main()

string
a="abcdefghigklmn";
string
b="def";
string
c="123";
string::size_type
idx;
idx=a.find(b);//在a中查找b.
if(idx
==
string::npos
)//不存在。
cout
<<
"not
found\n";
else//存在。
cout
<<"found\n";
idx=a.find(c);//在a中查找c。
if(idx
==
string::npos
)//不存在。
cout
<<
"not
found\n";
else//存在。
cout
<<"found\n";
return
0;

二、C语言风格。
在C语言中,字符串存储为字符数组,以"\0"结束。
在C的接口中,有strstr函数,可以在字符串中查找另一个字符串。
char
*
strstr(const
char
*str1,
const
char
*str2);
功能为在str1中查找str2,如果存在,那么返回查找到的起始指针,否则返回NULL。
参考代码:
#include

#include

#include

using
namespace
std;
int
main()

string
a="abcdefghigklmn";
char
*b="def";
char
*c="123";
if(strstr(a.c_str(),
b)
==
NULL)//在a中查找b,如果不存在,
cout
<<
"not
found\n";//输出结果。
else//否则存在。
cout
<<"found\n";
//输出结果。
if(strstr(a.c_str(),
c)
==
NULL)//在a中查找b,如果不存在,
cout
<<
"not
found\n";//输出结果。
else//否则存在。
cout
<<"found\n";
//输出结果。
return
0;

vba如何判断字符串里面是包含某个字符?

如题,要如何写才能判断 某个字符串里面包含的字符?然后从这个字符右边开始替换数值
例如:010-12345678 判断里面是否有“-”然后从“-”右边开始 替换4位数“****”
结果为:010-****5678

用instr()函数,语法是:
instr(起始位置,查找对象,查找目标,比较模式)
例如instr(1,“1234”,“3”,1)返回3,即按文本比较,在字符串中找到“3”,并返回其位置3。
参考技术A Sub 替换()
Dim i, x, n
For i = 1 To Range("a65536").End(xlUp).Row
x = Len(Cells(i, 1))
n = InStr(1, Cells(i, 1), "-", 1)
M = "abcd" "替换的数据
L = Left(Cells(i, 1), n)
R = Right(Cells(i, 1), x - 8)
Cells(i, 2) = L & M & R
Next
End Sub
参考下本回答被提问者采纳

以上是关于string怎么判断字符串是不是包含某几个字符的主要内容,如果未能解决你的问题,请参考以下文章