希望对同样学IT的你们有用
//indexOf()
//查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.
System.out.println(string.indexOf("b"));//indexOf(String str);返回结果:-1,"b"不存在
// 从第四个字符位置开始往后继续查找,包含当前位置
System.out.println(string.indexOf("a",3));//indexOf(String str, int fromIndex);返回结果:6
//(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99
// 从头开始查找是否存在指定的字符
System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7
System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7
//从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。
System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex);返回结果:6
System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex);返回结果:6
//这个就是灵活运用String类提供的方法,拆分提供的字符串。
//String s = "D:\\Android\\sdk\\add-ons";
//System.out.println(s);
//while (s.lastIndexOf("\\") > 0) {
// s = s.substring(0, s.lastIndexOf("\\"));
// System.out.println(s);
//}
}
//lastIndexOf()
strObj.lastIndexOf(substring[, startindex])
参数
strObj
必选项。String 对象或文字。
substring
必选项。要在 String 对象内查找的子字符串。
startindex
可选项。该整数值指出在 String 对象内进行查找的开始索引位置。假如省略,则查找从字符串的末尾开始。
说明
lastIndexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。假如没有找到子字符串,则返回 -1。
假如 startindex 是负数,则 startindex 被当作零。假如它比最大字符位置索引还大,则它被当作最大的可能索引。
从右向左执行查找。否则,该方法和 indexOf 相同。
下面的示例说明了 lastIndexOf 方法的用法:
function lastIndexDemo(str2)
{
var str1 = "BABEBIBOBUBABEBIBOBU"
var s = str1.lastIndexOf(str2);
return(s);
}
str2="OB";
s=17;
//replace()
public class Test {
public static void main(String args[]) {
String Str = new String("hello");
System.out.print("返回值 :" );
System.out.println(Str.replace('o', 'T'));
System.out.print("返回值 :" );
System.out.println(Str.replace('l', 'D'));
}
}
以上程序执行结果为:
返回值 :hellT
返回值 :heDDo
//split()
split 方法
将一个字符串分割为子字符串,然后将结果作为字符串数组返回。
stringObj.split([separator,[limit]])
stringObj
必选项。要被分解的 String 对象或文字。该对象不会被 split 方法修改。
separator
可选项。字符串或 正则表达式 对象,它标识了分隔字符串时使用的是一个还是多个字符。如果忽
略该选项,返回包含整个字符串的单一元素数组。
limit
可选项。该值用来限制返回数组中的元素个数。
说明:
split 方法的结果是一个字符串数组,在 stingObj 中每个出现 separator 的位置都要进行分解
。separator 不作为任何数组元素的部分返回。
示例1:
public class SplitDemo {
public static String[] ss = new String[20];
public SplitDemo() {
String s = "The rain in Spain falls mainly in the plain.";
// 在每个空格字符处进行分解。
ss = s.split(" ");
}
public static void main(String[] args) {
SplitDemo demo = new SplitDemo();
for (int i = 0; i < ss.length; i++)
System.out.println(ss[i]);
}
}
程序结果:
The
rain
in
Spain
falls
mainly
in
the
plain.
//startsWith()
startsWith() 方法用于检测字符串是否以指定的前缀开始。
语法
public boolean startsWith(String prefix, int toffset)
或
public boolean startsWith(String prefix)
参数
prefix -- 前缀。
toffset -- 字符串中开始查找的位置。
返回值
如果字符串以指定的前缀开始,则返回 true;否则返回 false。
实例
public class Test {
public static void main(String args[]) {
String Str = new String("www.runoob.com");
System.out.print("返回值 :" );
System.out.println(Str.startsWith("www") );
System.out.print("返回值 :" );
System.out.println(Str.startsWith("runoob") );
System.out.print("返回值 :" );
System.out.println(Str.startsWith("runoob", 4) );
}
}
以上程序执行结果为:
返回值 :true
返回值 :false
返回值 :true
//endsWith()
此方法测试字符串是否以指定的后缀 suffix 结束。
语法
此方法定义的语法如下:
[java] view plain copy
<span style="font-family:'Microsoft YaHei';font-size:18px;">public boolean endsWith(String suffix)</span>
返回值:
此方法如果参数所表示的字符序列是由该对象表示的字符序列的后缀返回true, 否则为false; 请注意,如果参数是空字符串或等于此String对象由equals(Object)方法确定结果为 true。
例子:
[java] view plain copy
public class Test{
public static void main(String args[]){
String Str = new String("This is really not immutable!!");
boolean retVal;
retVal = Str.endsWith( "immutable!!" );
System.out.println("Returned Value = " + retVal );
retVal = Str.endsWith( "immu" );
System.out.println("Returned Value = " + retVal );
}
}
结果:
[html] view plain copy
Returned Value = true
Returned Value = false
//substring()
substring() 方法返回字符串的子字符串。
语法
public String substring(int beginIndex)
或
public String substring(int beginIndex, int endIndex)
参数
beginIndex -- 起始索引(包括)。
endIndex -- 结束索引(不包括)。
返回值
子字符串。
实例
public class Test {
public static void main(String args[]) {
String Str = new String("www.runoob.com");
System.out.print("返回值 :" );
System.out.println(Str.substring(4) );
System.out.print("返回值 :" );
System.out.println(Str.substring(4, 10) );
}
}
以上程序执行结果为:
返回值 :runoob.com
返回值 :runoob
//toLowerCase()
toLowerCase() 方法用于将大写字符转换为小写。
语法
char toLowerCase(char ch)
参数
ch -- 要转换的字符。
返回值
返回转换后字符的小写形式,如果有的话;否则返回字符本身。
实例
public class Test {
public static void main(String args[]) {
System.out.println(Character.toLowerCase('a'));
System.out.println(Character.toLowerCase('A'));
}
}
以上程序执行结果为:
a
a
//toUpperCase()
toUpperCase() 方法将字符串小写字符转换为大写。
语法
public String toUpperCase()
或
public String toUpperCase(Locale locale)
参数
无
返回值
字符转换为大写后的字符串。
实例
public class Test {
public static void main(String args[]) {
String Str = new String("www.runoob.com");
System.out.print("返回值 :" );
System.out.println( Str.toUpperCase() );
}
}
以上程序执行结果为:
返回值 :WWW.RUNOOB.COM
//trim()
trim():去掉字符串首尾的空格。
[java] view plain copy
public static void main(String arg[]){
String a=" hello world ";
String b="hello world";
System.out.println(b.equals(a));
a=a.trim();//去掉字符串首尾的空格
System.out.println(a.equals(b));
}
执行结果:
a: hello world ,false
a:hello world,true
//valueOf()
valueOf() 方法用于返回给定参数的原生 Number 对象值,参数可以是原生数据类型, String等。
该方法是静态方法。该方法可以接收两个参数一个是字符串,一个是基数。
语法
该方法有以下几种语法格式:
static Integer valueOf(int i)
static Integer valueOf(String s)
static Integer valueOf(String s, int radix)
参数
i -- Integer 对象的整数。
s -- Integer 对象的字符串。
radix --在解析字符串 s 时使用的基数,用于指定使用的进制数。
返回值
Integer valueOf(int i):返回一个表示指定的 int 值的 Integer 实例。
Integer valueOf(String s):返回保存指定的 String 的值的 Integer 对象。
Integer valueOf(String s, int radix): 返回一个 Integer 对象,该对象中保存了用第二个参数提供的基数进行解析时从指定的 String 中提取的值。
实例
public class Test{
public static void main(String args[]){
Integer x =Integer.valueOf(9);
Double c = Double.valueOf(5);
Float a = Float.valueOf("80");
Integer b = Integer.valueOf("444",16); // 使用 16 进制
System.out.println(x);
System.out.println(c);
System.out.println(a);
System.out.println(b);
}
}
编译以上程序,输出结果为:
9
5.0
80.0
1092