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

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

2023/8/30 260次

香港云服务器的java中常用的字符串函数有:1.equals()函数,比较字符串是否相等;2.split()函数,拆分字符串;3.replace()函数,替换字符串;4.indexof()函数,返回字符索引;5.substring()函数,返回子字符串;6.concat()函数,连接字符串;7.toLowerCase()函数,将字符串转换为小写;8.toUpperCase()函数,将字符串转换为大写;9.length()函数,返回字符串长度;

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

}

}

一诺网络香港免备案专区,提供「香港增强云服务器」和「香港特惠云服务器」两种类型的高可用弹性计算服务,搭载新一代英特尔®至强®铂金处理器,接入CN2低延时高速回国带宽线路,网络访问顺滑、流畅。机房网络架构采用了BGP协议的解决方案可提供多线路互联融合网络,使得不同网络运营商线路的用户都能通过最佳路由实现快速访问。香港云服务器低至29/月,购买链接:https://www.enuoidc.com/vps.html?typeid=2