扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

java endswith函数怎么用

扬州沐宇科技
2023-11-10 09:16:23
Java

Java的endsWith()函数用于判断字符串是否以指定的后缀结束。它的用法如下:

String str = "Hello, World!";

// 判断字符串是否以指定的后缀结束
boolean endsWithExclamation = str.endsWith("!"); // true
boolean endsWithComma = str.endsWith(","); // false

System.out.println(endsWithExclamation);
System.out.println(endsWithComma);

在上面的例子中,endsWith()函数分别判断字符串str是否以感叹号!或逗号,结束。结果表明字符串str以感叹号!结束(为true),但不以逗号,结束(为false)。

扫码添加客服微信