java中substring的使用方法
str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字符串,将剩余字符串赋值给str;
str=str.substring(int beginIndex,int endIndex);截取str中从beginIndex開始至endIndex结束时的字符串,并将其赋值给str;
下面是一段演示程序:
public class StringDemo{
public static void main(String agrs[]){
String str="this is my original string";
String toDelete=" original";
if(str.startsWith(toDelete))
str=str.substring(toDelete.length());
else
if(str.endsWith(toDelete))
str=str.substring(0, str.length()-toDelete.length());
else
{
int index=str.indexOf(toDelete);
if(index!=-1)
{
String str1=str.substring(0, index);
String str2=str.substring(index+toDelete.length());
str=str1+str2;
}
else
System.out.println("string /""+toDelete+"/" not found");
}
System.out.println(str);
}
}
(原文引用自:http://hi.baidu.com/ccsos/blog/item/42ff84afe6e62bcd7dd92a62.html)
补充:str=str.substring(int beginIndex,int endIndex);中终于得到的值:
beginIndex =< str的值 < endIndex
以上补充内容是我自己曾经的一点理解
近日在API中看到对它的注解,
把它公布在以下以便很多其它的和我一样的刚開始学习的人更好的理解上面的程序
substring
public String substring(int beginIndex,
int endIndex)
- 返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的
beginIndex处開始,一直到索引endIndex - 1处的字符。因此,该子字符串的长度为endIndex-beginIndex。
演示样例:
"hamburger".substring(4, 8) returns "urge"
"smiles".substring(1, 5) returns "mile"
- 參数:
beginIndex- 開始处的索引(包含)。endIndex- 结束处的索引(不包含)。- 返回:
- 指定的子字符串。
- 抛出:
IndexOutOfBoundsException- 假设beginIndex为负,或endIndex大于此String对象的长度,或beginIndex大于endIndex。
java中substring的使用方法的更多相关文章
- 【Java】Java中常用的String方法
本文转载于:java中常用的String方法 1 length()字符串的长度 String a = "Hello Word!"; System.out.println(a.len ...
- java中File的delete()方法删除文件失败的原因
java中File的delete()方法删除文件失败的原因 学习了:http://hujinfan.iteye.com/blog/1266387 的确是忘记关闭了: 引用原文膜拜一下: 一般来说 ja ...
- java中substring和indexof() 和lastindexof()
java中substring和indexof() 和lastindexof() str=str.substring(int beginIndex);截取掉str从首字母起长度为beginIndex的字 ...
- Java中Set的contains()方法
Java中Set的contains()方法 -- hashCode与equals方法的约定及重写原则 翻译人员: 铁锚 翻译时间: 2013年11月5日 原文链接: Java hashCode() a ...
- [java,2017-05-16] java中清空StringBuffer的方法以及耗费时间比较
java中清空StringBuffer的方法,我能想到的有4种: 1. buffer.setLength(0); 设置长度为0 2. buffer.delete(0, buffer.length() ...
- java中BorderLayout的使用方法
相关设置: 使用BorderLayout布局上下左右中布局5个按键,单击中间的那个按键时就关闭窗口 代码: /**** *java中BorderLayout的使用方法 * 使用BorderLayout ...
- Oracle trunc()函数,decode()函数,substr函数,GREATEST函数,java中substring函数的用法
--Oracle trunc()函数的用法/**************日期********************/1.select trunc(sysdate) from dual --2013- ...
- Java中Set的contains()方法——hashCode与equals方法的约定及重写原则
转自:http://blog.csdn.net/renfufei/article/details/14163329 翻译人员: 铁锚 翻译时间: 2013年11月5日 原文链接: Java hashC ...
- java中equals和hashCode方法随笔二
前几天看了篇关于java中equals和hashCode方法的解析 1.Object类中的equals方法和hashCode方法. Object类中的equals和hashCode方法简单明了,所有的 ...
随机推荐
- grep, egrep, fgrep笔记
grep, egrep, fgrep grep: 根据模式搜索文本,并将符合模式的文本行显示出来.Pattern: 文本字符和正则表达式的元字符组合而成匹配条件 grep [options] PATT ...
- java面试题集1
一:单选题 下列哪一种叙述是正确的(D )A. abstract修饰符可修饰字段.方法和类B. 抽象方法的body部分必须用一对大括号{ }包住C. 声明抽象方法,大括号可有可无D. 声明抽象方法不可 ...
- jquery实现仿商品星级评价
一,HTML部分 <div id="rating-star"> <a href="#">0</a> < ...
- php常用的操作
一. php配置 1.禁止一些函数disable_functions = eval,assert,popen,passthru,escapeshellarg,escapeshellcmd,passt ...
- Asp.net MVC5中Html.DropDownList的使用
一.静态下拉列表项的绑定 在下拉列表中绑定静态项,我们可以通过 SelectListItem 的集合作为数据源的下拉列表. @Html.DropDownList("dropRoles&quo ...
- TableLayout属性
整理于http://naotu.baidu.com/file/e5880b84b1a906838116f7a45f58de78
- MYSQL5.6数据库ZIP安装以及VS中使用注意事项
先挂资源,下载地址: http://download.softagency.net/MySQL/Downloads/. 找到MYSQL5.6,两个版本看系统而定我下的是64位的:http://down ...
- MySql小知识点
1.查看MySql是什么编码 show create table tablename;
- input type file onchange上传文件的过程中,遇到同一个文件二次上传无效的问题。
不要采用删除当前input[type=file]这个节点,然后再重新创建dom这种方案,这样是不合理的.解释如下:input[type=file]使用的是onchange去做,onchange监听的为 ...
- 正式学习React(一) 开始学习之前必读
为什么要加这个必读!因为webpack本身是基于node环境的, 里面会涉及很多路径问题,我们可能对paths怎么写!webpack又是怎么找到这些paths的很迷惑. 本文是我已经写完正式学习Rea ...