编写一个Java应用程序,判断两个字符串是否相同,判断字符串的前缀、后缀是否和某个字符串相同,按字典顺序比较两个字符串的大小关系,检索字符串,创建字符串,将数字型字符串转换为数字,将字符串存放到数组中,用字符数组创建字符串。
时间: 2024-01-01 15:03:42 浏览: 110
判断两个字符串是否相同:
```java
public static boolean isSame(String str1, String str2) {
return str1.equals(str2);
}
```
判断字符串的前缀、后缀是否和某个字符串相同:
```java
public static boolean isPrefixOrSuffix(String str, String prefixOrSuffix) {
return str.startsWith(prefixOrSuffix) || str.endsWith(prefixOrSuffix);
}
```
按字典顺序比较两个字符串的大小关系:
```java
public static int compare(String str1, String str2) {
return str1.compareTo(str2);
}
```
检索字符串:
```java
public static int search(String str, String target) {
return str.indexOf(target);
}
```
创建字符串:
```java
String str = "Hello, world!";
```
将数字型字符串转换为数字:
```java
String numStr = "123";
int num = Integer.parseInt(numStr);
```
将字符串存放到数组中:
```java
String[] arr = {"apple", "banana", "orange"};
```
用字符数组创建字符串:
```java
char[] charArr = {'H', 'e', 'l', 'l', 'o'};
String str = new String(charArr);
```
阅读全文
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![text/x-java](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)