Javaindexof(java中indexof的用法)

Javaindexof(java中indexof的用法)

扫码添加渲大师小管家,免费领取渲染插件、素材、模型、教程合集大礼包!

这个方法有以下不同的变体:

public int indexOf(int ch): 返回此字符串指定字符第一次出现,或如果该字符不出现-1处的索引。
public int indexOf(int ch, int fromIndex): 返回索引这个字符串中指定字符第一次出现处,开始搜索指定的索引处或-1,如果该字符不会出现。
int indexOf(String str): 返回此字符串指定子字符串的第一次出现处的索引。如果不出现作为一个子串,则返回-1.
int indexOf(String str, int fromIndex): 返回索引这个字符串中指定子字符串的第一次出现处,从指定的索引处。如果它不出现,则返回-1.
语法

此方法定义的语法如下:

public int indexOf(int ch )
or
public int indexOf(int ch, int fromIndex)
or
int indexOf(String str)
or
int indexOf(String str, int fromIndex)
参数

这里是参数的细节:

ch -- 一个字符.
fromIndex -- 从这个索引开始搜索.
str -- 一个字符串.
返回值:

看描述说明
例子:

import java.io.*;
public class Test {
public static void main(String args[]) {
String Str = new String("Welcome to Tutorialspoint.com");
String SubStr1 = new String("Tutorials");
String SubStr2 = new String("Sutorials");
System.out.print("Found Index :" );
System.out.println(Str.indexOf( 'o' ));
System.out.print("Found Index :" );
System.out.println(Str.indexOf( 'o', 5 ));
System.out.print("Found Index :" );
System.out.println( Str.indexOf( SubStr1 ));
System.out.print("Found Index :" );
System.out.println( Str.indexOf( SubStr1, 15 ));
System.out.print("Found Index :" );
System.out.println(Str.indexOf( SubStr2 ));
}
}
这将产生以下结果:

Found Index :4
Found Index :9
Found Index :11
Found Index :-1
Found Index :-1

java.lang.StringBuilder.setLength() 方法将字符序列的长度。该序列被改变为其长度由参数指定一个新的字符序列。

如果newLength参数大于或等于当前的长度,足以让空字符('u0000“)附加以使长度成为newLength参数。

声明

以下是
java.lang.StringBuilder.setLength()方法的声明

public void setLength(int newLength)
参数

newLength -- 这是新的长度。
返回值

此方法不返回任何值。

异常

IndexOutOfBoundsException -- 如果newLength参数为负。
例子

下面的例子显示
java.lang.StringBuilder.setLength()方法的使用。

package com.yiibai;
import java.lang.*;
public class StringBuilderDemo {
public static void main(String[] args) {

StringBuilder str = new StringBuilder("tutorials");
System.out.println("string = " + str);
// length of StringBuilder
System.out.println("length = " + str.length());

// set the length of StringBuilder to 5
str.setLength(5);

// print new StringBuilder value after changing length
System.out.println("After set, string = " + str);
// length of StringBuilder after changing length
System.out.println("length = " + str.length());
}
}
让我们来编译和运行上面的程序,这将产生以下结果:

string = tutorials
length = 9
After set, string = tutor
length = 5

分享到 :
相关推荐

美国服务器有哪些优势(美国服务器有哪些优势和劣势)

美国服务器的优势有:1。美国服务器不用备案。网站内容限制少;2。美国服务器有充足的带...

为什么选择香港服务器搭建互联网金融平台

选择香港服务器作为互联网金融平台的优势:1.香港服务器走的线路一般是直连线路。受到[...

免费ASP源码(asp网站免费源码)

有很多工具可以书写ASP源代码,我使用Windows自带的记事本来编写ASP源代码。...

免费的asp空间试用要注意什么(免费的asp空间试用要注意什么)

免费的asp空间试用要注意:1。网站空间的尺寸。根据网站类型整体规划和浏览总流量选择...

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注