首页>>帮助中心>>香港服务器java字符串函数有哪些

香港服务器java字符串函数有哪些

2023/6/12 345次

香港服务器java中常用的字符串函数有以下几种

1.equals()函数

javaequals()函数的作用是用于比较两个字符串的变量值是否相等。

s1 = new String("abc");s2 = new String("abc");s1.equals(s2) //返回 true

2.split()函数

javasplit()函数的作用是用于根据给定的正则表达式对字符串进行拆分。

String str = "one two three, four";

String[] tokens = str.split(" ");

for (String s: tokens)

System.out.println(s);

3.replace()函数

javareplace()函数的作用是用于将字符串中某一个相同的信息替换为另外一个字符串。

String a = "hello wrold!";

String b = "China!";

a = a.replace("wrold!",b);

System.out.println(a); //返回hello China!

4.indexof()函数

javaindexof()函数的作用是用于返回一个字符在字符串中的索引,如字符串中没有该字符,则返回-1

String str = "01234567890123456789";System.out.println(str.indexOf("123")); //返回 1System.out.println(str.indexOf("321")); //返回 -1

5.substring()函数

javasubstring()函数的作用是用于返回字符串的子字符串。

public class RunoobTest {

public static void main(String args[]) {

String Str = new String("This is text");

System.out.print("返回值 :" );//返回is text

System.out.println(Str.substring(4) );

System.out.print("返回值 :" );//返回is te

System.out.println(Str.substring(4, 10) );

}

}

6.concat()函数

javaconcat()函数的作用是用于将指定的字符串参数连接到字符串上。

public class Test {

public static void main(String args[]) {

String s = "你好";

s = s.concat("世界");

System.out.println(s);

}

}

输出结果为:

你好世界

7.toLowerCase()函数

javatoLowerCase()函数的作用是将字符串大写字符转换为小写。

public class Test {

public static void main(String args[]) {

System.out.println(Character.toLowerCase('HELLO WORLD'));

}

}

输出结果为:

hello world

8.toUpperCase()函数

javatoUpperCase()函数的作用是将字符串小写字符转换为大写。

public class Test {

public static void main(String args[]) {

System.out.println(Character.toUpperCase('hello world'));

}

}

输出结果为:

HELLO WORLD

9.length()函数

javalength()函数的作用是用于返回字符串的长度。

public static void main(String args[]) {

String site = "hello world";

int len = site.length();

System.out.println( len ); //返回12

}

}

购买使用一诺网络香港服务器,可以极大降低初创企业、中小企业以及个人开发者等用户群体的整体IT使用成本,无需亲自搭建基础设施、简化了运维和管理的日常工作量,使用户能够更专注于自身的业务发展和创新。香港服务器低至29元/月,购买链接:https://www.enuoidc.com/vps.html?typeid=2