java - 返回从 String 的输入参数索引开始的字符串
问题描述
解决方案
return 语句由类似于 if-else 条件的三元条件组成。如果索引大于字符串的长度,它将返回“”,否则它将从输入索引返回子字符串
public class Test {
public static void main(String[] args) {
System.out.println("String that begins at 2 index : "+getTheString(2)); // Output waii
System.out.println("String that begins at 8 index : "+getTheString(8));// Output ""
}
public static String getTheString(int index) {
String str = "Hawaii";
return index > str.length() ? "" : str.substring(index);
}
}
推荐阅读
- r - 启动 Rbase docker 容器时如何启动 cron
- python - Python:CSV 文件未正确读取
- wpf - System.Windows.Media.Transform.Inverse 在什么情况下会返回 null
- java - Java 中高效的 Zip 文件流
- php - 字符串的 === 运算符的 PHP 内部实现
- html - 如何根据html中的div大小调整图像
- python - 在 Mac 上的 Jupyter Notebook 和西班牙语 ISO 中将多行代码转换为注释
- dataframe - 将 Dataframe 写入 Azure SQL Server 表的最佳实践?
- r - 使用特定条件在 R 中拆分字符串
- c# - 以非升序从大文件中读取多行