java string.indexof(string)_Java StringBuffer indexOf()方法

Java StringBuffer indexOf()方法

java.lang.StringBuffer.indexOf(String str, int fromIndex) 方法返回此字符串指定的子字符串的第一次出现的索引,从指定的索引处。fromIndex参数是开始搜索的索引。

1 语法

public int indexOf(String str, int fromIndex)

2 参数

str : 这是要搜索的子字符串。

fromIndex : 从这里开始搜索的索引。

3 返回值

此方法返回从指定的索引处此字符串指定的子字符串第一次出现的索引。

4 示例

package com.yiidian;

/**

* 一点教程网: http://www.yiidian.com

*/

/**

* Java Enum compareTo()方法

*/

import java.lang.*;

public class StringBufferDemo {

public static void main(String[] args) {

StringBuffer buff = new StringBuffer("programming language");

System.out.println("buffer = " + buff);

// returns the index of the specified substring

System.out.println("Index of substring = " + buff.indexOf("age"));

/* returns the index of the specified substring, starting at

the specified index */

System.out.println("Index of substring = " + buff.indexOf("am",2));

/* returns -1 as the substring is not found starting at the

specified index */

System.out.println("Index of substring = " + buff.indexOf("am",10));

}

}

输出结果为:

buffer = programming language

Index of substring = 17

Index of substring = 5

Index of substring = -1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值