求字符串中指定开始和结束字符串的子串数量
来源:互联网 发布:淘宝网店助理 编辑:程序博客网 时间:2024/05/18 00:32
例如:String s = abababab;String sFrom = a;String sTo = b;那么结果为4;
例如:S = aba; sFrom = ab; sTo = ba;那么结果为1;
例如:S = round; sFrom = ro;sTo = on;那么结果为0。
上代码:
import java.util.Scanner;public class test {public static void main(String[] args) {// TODO Auto-generated method stubScanner sc = new Scanner(System.in); String s = sc.nextLine(); String sFrom = sc.nextLine();String sTo = sc.nextLine();System.out.println(findNumOfSubstring(s,sFrom,sTo));}public static int findNumOfSubstring(String s,String sFrom,String sTo){if(s == null || s.length() == 0){return 0;}int fromIndex = 0;int toIndex = s.length()-1;int count = 0;while(fromIndex < toIndex){while(s.indexOf(sFrom,fromIndex) == -1 && fromIndex < toIndex){fromIndex++;} while(s.indexOf(sTo, toIndex) == -1 && fromIndex < toIndex){ toIndex--; } if(fromIndex < toIndex){ count++; } fromIndex++; toIndex--;}return count;}}
0 0
- 求字符串中指定开始和结束字符串的子串数量
- Java删除字符串中指定开始到指定结束之间的内容
- 字符串中指定长度的子字符串的提取
- C语言版字符串中获得子串的函数,获取字符串中指定位置的子串
- 查找字符串中指定的子串的首次出现的位置
- 字符串中指定字符串的替换
- 删除字符串中指定的字符串
- ORACLE_截取字符串中指定的字符和值
- 求一个字符串里含有的子字符串的数量
- C# 获取一年中指定的一周的开始日期和结束日期的通用方法
- shell 字符串 开始 结束 子字符串
- 去除字符串中指定字符的方法
- 查找字符串中指定字符的个数
- 删除字符串中指定位置的字符
- 删除字符串中指定位置的字符
- 删除字符串中指定的字符
- 改变字符串中指定字符的颜色
- 删除字符串中指定的字符
- Spark GraphX相关使用方法
- 值类型和引用类型
- POJ 题目3744(矩阵优化概率DP)
- LeetCode之Unique Binary Search Trees & Unique Binary Search Trees II
- Eclipse中在web项目中添加hibernate
- 求字符串中指定开始和结束字符串的子串数量
- 设计模式问答系列(2)----工厂模式
- Android 的消息机制(Handler消息传递机制)
- opencv学习日常之Mat的代数运算
- 《C++ Primer》笔记
- hdu1219 AC Me
- 《机器学习实战》——logistic回归
- 使用DNS服务器实现负载均衡
- iOS——instancetype浅析