【Java】判断字符串是否包含子字符串
JAVA里面判断:
public static void main(String[] args) {
String str="ABC_001";
if(str.indexOf("ABC")!=-1){
System.out.println("包含");
}else{ System.out.println("不包含");
}
}
JavaScript里面判断:
function IndexDemo(str2){
var str1
= "BABEBIBOBUBABEBIBOBU"
var s
= str1.indexOf(str2);
return(s);
}
var Cts
= "bblText"; if(Cts.indexOf("Text")
> 0 )
{
alert('Cts中包含Text字符串');
}
截取相关:
java截取相关
1、length() 字符串的长度
例:char chars[]={'a','b'.'c'};
String s=new String(chars);
int len=s.length();
2、charAt() 截取一个字符
例:char ch;
ch="abc".charAt(1); 返回'b'
3、getChars() 截取多个字符
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart指定了子串开始字符的下标,sourceEnd指定了子串结束后的下一个字符的下标。因此,子串包含从sourceStart到sourceEnd-1的字符。接收字符的数组由target指定,target中开始复制子串的下标值是targetStart。
例:String s="this is a demo of the getChars method.";
char buf[]=new char[20];
s.getChars(10,14,buf,0);
4、getBytes()
替代getChars()的一种方法是将字符存储在字节数组中,该方法即getBytes()。
5、toCharArray()
6、equals()和equalsIgnoreCase() 比较两个字符串
7、regionMatches() 用于比较一个字符串中特定区域与另一特定区域,它有一个重载的形式允许在比较中忽略大小写。
boolean regionMatches(int startIndex,String str2,int str2StartIndex,int numChars)
boolean regionMatches(boolean ignoreCase,int startIndex,String str2,int str2StartIndex,int numChars)
8、startsWith()和endsWith()
startsWith()方法决定是否以特定字符串开始,endWith()方法决定是否以特定字符串结束
9、equals()和==
equals()方法比较字符串对象中的字符,==运算符比较两个对象是否引用同一实例。
例:String s1="Hello";
String s2=new String(s1);
s1.eauals(s2); //true
s1==s2;//false
10、compareTo()和compareToIgnoreCase() 比较字符串
11、indexOf()和lastIndexOf()
indexOf() 查找字符或者子串第一次出现的地方。
lastIndexOf() 查找字符或者子串是后一次出现的地方。
12、substring()
它有两种形式,第一种是:String substring(int startIndex)
第二种是:String substring(int startIndex,int endIndex)
13、concat() 连接两个字符串
14 、replace() 替换
它有两种形式,第一种形式用一个字符在调用字符串中所有出现某个字符的地方进行替换,形式如下:
String replace(char original,char replacement)
例如:String s="Hello".replace('l','w');
第二种形式是用一个字符序列替换另一个字符序列,形式如下:
String replace(CharSequence original,CharSequence replacement)
15、trim() 去掉起始和结尾的空格
16、valueOf() 转换为字符串
17、toLowerCase() 转换为小写
18、toUpperCase() 转换为大写
19、StringBuffer构造函数
StringBuffer定义了三个构造函数:
StringBuffer()
StringBuffer(int size)
StringBuffer(String str)
StringBuffer(CharSequence chars)
(1)、length()和capacity()
一个StringBuffer当前长度可通过length()方法得到,而整个可分配空间通过capacity()方法得到。
(2)、ensureCapacity() 设置缓冲区的大小
void ensureCapacity(int capacity)
(3)、setLength() 设置缓冲区的长度
void setLength(int len)
(4)、charAt()和setCharAt()
char charAt(int where)
void setCharAt(int where,char ch)
(5)、getChars()
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
(6)、append() 可把任何类型数据的字符串表示连接到调用的StringBuffer对象的末尾。
例:int a=42;
StringBuffer sb=new StringBuffer(40);
String s=sb.append("a=").append(a).append("!").toString();
(7)、insert() 插入字符串
StringBuffer insert(int index,String str)
StringBuffer insert(int index,char ch)
StringBuffer insert(int index,Object obj)
index指定将字符串插入到StringBuffer对象中的位置的下标。
(8)、reverse() 颠倒StringBuffer对象中的字符
StringBuffer reverse()
(9)、delete()和deleteCharAt() 删除字符
StringBuffer delete(int startIndex,int endIndex)
StringBuffer deleteCharAt(int loc)
(10)、replace() 替换
StringBuffer replace(int startIndex,int endIndex,String str)
(11)、substring() 截取子串
String substring(int startIndex)
String substring(int startIndex,int endIndex)
【Java】判断字符串是否包含子字符串的更多相关文章
- 如何在 JavaScript 中检查字符串是否包含子字符串?
如何在 JavaScript 中检查字符串是否包含子字符串? // var test4 = _.includes(string, substring); 该方法需要此文件 <script src ...
- python判断字符串是否包含子字符串
python的string对象没有contains方法,不可以使用string.contains的方法判断是否包含子字符串,但是python有更简单的方法来替换contains函数 python的st ...
- Java-确定字符串是否包含子字符串
利用String自带的函数和正则来实现 package com.tj; public class MyClass implements Cloneable { public static void m ...
- python 判断字符串是否包含子字符串
第一种方法:in string = 'helloworld' if 'world' in string: print 'Exist' else: print 'Not exist' 第二种方法:fin ...
- python判断字符串中是否包含子字符串
python判断字符串中是否包含子字符串 s = '1234问沃尔沃434' if s.find('沃尔沃') != -1: print('存在') else: print('不存在' ...
- 【功能代码】---3 JS判断字符串是否包含某个字符串
JS判断字符串是否包含某个字符串 var str ="abc"; if(str.indexOf("bc")>-1){ alert('str中包含bc字符串 ...
- 算法之暴力破解和kmp算法 判断A字符串是否包含B字符串
我们都知道java中有封装好的方法,用来比较A字符串是否包含B字符串 如下代码,contains,用法是 str1.contains(str2), 这个布尔型返回,存在返回true,不存在返回fals ...
- Mysql查找如何判断字段是否包含某个字符串
Mysql查找如何判断字段是否包含某个字符串 有这样一个需求,在Mysql数据库字符串字段(权限)中,用户有多个不同的邮箱,分别被‘,’分开,现在要取出某个邮箱的所有成员列表. 假设有个表: ...
- mysql判断表里面一个逗号分隔的字符串是否包含单个字符串、查询结果用逗号分隔
1.mysql判断表里面一个逗号分隔的字符串是否包含单个字符串 : FIND_IN_SET select * from tablename where FIND_IN_SET(传的参数,匹配字段) 例 ...
随机推荐
- WEB项目日志分析系统思考
一.为什么需要日志分析系统 对ETL系统中数据转换和存储操作的相关日志进行记录以及实时分析有助于我们更好的观察和监控ETL系统的相关指标(如单位时间某些操作的处理时间),发现系统中出现的缺陷和性能瓶颈 ...
- [BZOJ4011][HNOI2015]落忆枫音-[dp乱搞+拓扑排序]
Description 传送门 Solution 假如我们的图为DAG图,总方案数ans为每个点的入度In相乘(不算1号点).(等同于在每个点的入边选一条边,最后一定构成一棵树). 然而如果加了边x- ...
- python 多线程笔记(6)-- 生产者/消费者模式(续)
用 threading.Event() 也可以实现生产者/消费者模式 (自己拍脑袋想出来的,无法知道其正确性,请大神告知为谢!) import threading import time import ...
- HTML中CSS入门基础
HTML.CSS 实用css有三种格式:内嵌:内联:外部: 分类:内联:写在标记的属性位置,优先级最高,重用性最差内嵌:写在页面的head中,优先级第二,重用性一般外部:写在一个以css结尾的文件中, ...
- info信息的获取
一.绝对路径(_SERVER[“SCRIPT_FILENAME”])这个是最常用,也是最有效的一个办法,找到phpinfo()页面可以直接找到网站的绝对路径,对于写shell和信息搜集是必不可少的.二 ...
- Eclipse 无法编译 或 提示“错误: 找不到或无法加载主类”
project显示一个红色叹号,通常是.jar文件缺失,在下面找到配置 在libraries中添加add External JARs添加.jar文件
- 【BUG】12小时制和24小时制获取当天零点问题
[BUG]12小时制和24小时制获取当天零点问题 最近在写定时服务的时候,要获取当天的零点这个时间,但是是这样获取的 DateTime dt = DateTime.Parse(DateTime.Now ...
- 228. [LeetCode] Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- Python常用模块之VideoCapture
官方网址:http://videocapture.sourceforge.net/ 功能介绍: VideoCapture是Win32版Python的一个扩展,可以访问视频采集设备(如USB摄像头) ...
- underscore.js源码解析(四)
没看过前几篇的可以猛戳这里: underscore.js源码解析(一) underscore.js源码解析(二) underscore.js源码解析(三) underscore.js源码GitHub地 ...