-
Java获取指定字符串出现次数的方法
2020-08-28 14:11:17主要为大家详细介绍了Java获取指定字符串出现次数的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 -
Java 获取指定字符串出现的次数
2019-02-10 19:26:09Java 获取指定字符串出现的次数Java中 获取指定字符串在另一个字符串中出现的次数
方式一
/** * @param args */public static void main(String[] args) { String srcText = "Hello World"; String findText = "e"; int num = appearNumber(srcText, findText); System.out.println(num);}/** * 获取指定字符串出现的次数 * * @param srcText 源字符串 * @param findText 要查找的字符串 * @return */public static int appearNumber(String srcText, String findText) { int count = 0; Pattern p = Pattern.compile(findText); Matcher m = p.matcher(srcText); while (m.find()) { count++; } return count;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
方式二
/** * @param args */public static void main(String[] args) { String srcText = "Hello World"; String findText = "e"; int num = appearNumber(srcText, findText); System.out.println(num);}/** * public int indexOf(int ch, int fromIndex) * 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索 * * @param srcText * @param findText * @return */public static int appearNumber(String srcText, String findText) { int count = 0; int index = 0; while ((index = srcText.indexOf(findText, index)) != -1) { index = index + findText.length(); count++; } return count;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
作者:itmyhome
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
-
java字符串出现的位置_Java 获取指定字符串出现的次数及第N次出现的位置
2021-02-12 18:37:13Java中 获取指定字符串在另一个字符串中出现的次数方式一/*** @param args*/public static void main(String[] args) {String srcText = "Hello World";String findText = "e";int num = appearNumber(srcText, ...Java中 获取指定字符串在另一个字符串中出现的次数
方式一
/**
* @param args
*/
public static void main(String[] args) {
String srcText = "Hello World";
String findText = "e";
int num = appearNumber(srcText, findText);
System.out.println(num);
}
/**
* 获取指定字符串出现的次数
*
* @param srcText 源字符串
* @param findText 要查找的字符串
* @return
*/
public static int appearNumber(String srcText, String findText) {
int count = 0;
Pattern p = Pattern.compile(findText);
Matcher m = p.matcher(srcText);
while (m.find()) {
count++;
}
return count;
}
方法二
/**
* @param args
*/
public static void main(String[] args) {
String srcText = "Hello World";
String findText = "e";
int num = appearNumber(srcText, findText);
System.out.println(num);
}
/**
* public int indexOf(int ch, int fromIndex)
* 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
*
* @param srcText
* @param findText
* @return
*/
public static int appearNumber(String srcText, String findText) {
int count = 0;
int index = 0;
while ((index = srcText.indexOf(findText, index)) != -1) {
index = index + findText.length();
count++;
}
return count;
}
获取第N次出现时的位置
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo {
//判断"Ab2Ad3A4"中"A"出现第二次的位置
public static void main(String[] args) {
String str = "Ab2Ad3A4";
Pattern pattern = Pattern.compile("A");
Matcher findMatcher = pattern.matcher(str);
int number = 0;
while(findMatcher.find()) {
number++;
if(number == 2){//当“A”第二次出现时停止
break;
}
}
int i = findMatcher.start();//“A”第二次出现的位置
System.out.println("'A'第二次出现的位置是:"+i);
}
}
-
java 获取指定字符串出现的次数 以及出现的位置
2020-07-28 18:22:17* 获取指定字符串出现的次数 * * @param srcText 源字符串 * @param findText 要查找的字符串 * @return */ public static int appearNumber(String srcText, String findText) { int count = 0; Pattern p.../** * 获取指定字符串出现的次数 * * @param srcText 源字符串 * @param findText 要查找的字符串 * @return */ public static int appearNumber(String srcText, String findText) { int count = 0; Pattern p = Pattern.compile(findText); Matcher m = p.matcher(srcText); while (m.find()) { count++; int i = m.start(); System.out.println("第"+count+"次,位置:"+i); } return count; }
-
java获取指定字符串的下标从零开始
2020-01-07 11:44:35废话不多说上代码: ... //这里传了三个参数ABCDEFGHIJKLMNOPQRSTUVWXYZ表示你的一条字符串,Z表示我要获取它的下标,1表示我这里字符串没有重复的,如果有就输入2表示选择字符串中第二个Z的下标 int t =...废话不多说上代码:
public static void main(String[] args) throws UnsupportedEncodingException { //这里传了三个参数ABCDEFGHIJKLMNOPQRSTUVWXYZ表示你的一条字符串,Z表示我要获取它的下标,1表示我这里字符串没有重复的,如果有就输入2表示选择字符串中第二个Z的下标 int t = getCharacterPosition("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "Z", 1); System.out.println(t); } public static int getCharacterPosition(String url, String s, int i) { Matcher slashMatcher = Pattern.compile(s).matcher(url); int mIdx = 0; while (slashMatcher.find()) { mIdx++; //当"Z"符号第i次出现的位置 if (mIdx == i) { break; } } return slashMatcher.start(); }
-
java获取指定字符串的下一个
2016-06-30 13:46:00* 实现字符串+1 * 如ab9Z变为ac0A, * @param a * @return */ public static String returnString (String a) { //判断是否为纯数字 qq = a; try{ Integer pcode = Integer.valueOf(qq).int... -
Java 获取指定字符串出现的次数及第N次出现的位置
2020-04-24 17:45:35public void c(){ String str = "abcabcabcabdcabc"; Pattern pattern = Pattern.compile("a"); Matcher findMatcher = pattern.matcher(str); int number = 0; while(findMatcher.find()) { ... -
java 获取指定字符后的字符串
2020-01-04 18:12:51str.substring(0, str.indexOf(":"))//截取第一个:之前的所有字符串 str.substring(str.indexOf(":"))//截取第一个:之后的所有字符并且包括本身 把上面的代码替换成我们刚刚的方法: String aa="下单数量:1... -
java 获取字符串_Java获取字符串信息
2021-02-12 09:18:33//查找字符在字符串中的位置,该方法用于返回参数字符串s在指定字符串中首次出现的索引位置,当调用字符串的indexOf()方法时,会从当前 的字符串的开始位置搜索s的位置;如果没有检索到字符串s,该方法返回值是-1例:... -
Java——String练习(一)获取指定字符串中,另一个字符串出现的次数,并输出每次字符串出现位置的下标;...
2020-04-23 15:07:101、获取指定字符串中,另一个字符串出现的次数,并输出每次字符串出现位置的下标; 2、将字指定符串的首字母转成大写,字符串中的其他字母转成小写; 3、获取指定字符串中,大写字母、小写字母、阿拉伯数字、其他字符... -
Java实现获取指定字符串中某个子字符串出现的个数
2018-04-17 18:09:03public class findStrNum { public static void main(String[] args) { String str = "a abc fo abc gsdfsdsbcsgsod abcsgs abc"; int fromIndex = 0; int count = 0;... while... -
JAVA中截取字符串中指定字符串
2019-09-06 15:14:06JAVA中截取字符串中指定字符串 举个例子,需要截取“abcdef”中的“cde”。 场景1:获取该字符串的下标。输出“cde”。 public static void main(String[] args) { // TODO Auto-generated method stub String ... -
java 获取字符串的位置_Java如何获取字符在字符串中的位置
2021-02-12 16:25:21Java中要获取字符在字符串中的位置,可以通过indexOf()函数来实现。函数语法:indexOf() 函数有以下四种形式:public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的...
-
MySQL 存储过程(创建海量数据实验环境)
-
投标方法论
-
python地理空间:地理空间发现项目的集合-源码
-
RPS:剪刀石头布-源码
-
Delta Lake 在 Soul 的应用实践
-
labview串口测试.zip
-
基于微信的同城小程序、校园二手交易小程序 毕业设计毕设源码使用教程
-
Microsoft Outlook更新以后——助你效率升级
-
蓝桥杯单片机历届模拟题.zip
-
MySQL 备份与恢复详解(高低版本 迁移;不同字符集 相互转换;表
-
mysqld: Can‘t create/write to file ‘xxxxxx‘ (OS errno 2 - No such file or directory)
-
C#抓取网络上的数据 WebClient
-
MySQL 高可用工具 heartbeat 实战部署详解
-
Linux主要概念
-
用微服务spring cloud架构打造物联网云平台
-
German_LocalePack_de_DE:Magento社区版的Deutsche Sprachpaket-源码
-
3D模型检索的深点卷积方法
-
pstools.tar.gz
-
docker-compose更新单独某个服务镜像
-
MySQL 函数、用户自定义函数