java创建顺序串新建串和替换串操作
时间: 2024-06-10 10:09:30 浏览: 13
1. 创建顺序串
在Java中,创建顺序串可以使用字符串字面量或者使用String类的构造函数。例如:
```java
String str1 = "hello world"; // 字符串字面量方式创建
String str2 = new String("hello world"); // 使用构造函数创建
```
2. 新建串
可以使用String类提供的substring()方法来新建一个子串。该方法接受两个参数,分别表示子串的起始位置和结束位置(不包括结束位置)。例如:
```java
String str = "hello world";
String subStr = str.substring(0, 5); // 新建子串"hello"
```
3. 替换串
可以使用String类提供的replace()方法来替换字符串中指定的字符或字符串。该方法接受两个参数,分别表示要替换的字符或字符串和替换后的字符或字符串。例如:
```java
String str = "hello world";
String newStr = str.replace("world", "java"); // 将"world"替换为"java"
```
相关问题
代码:java创建顺序串新建串和替换串操作
创建顺序串:
```java
public class SeqString {
private char[] data; // 用于存储串数据的数组
private int length; // 串的长度
public SeqString() {
data = new char[0];
length = 0;
}
public SeqString(char[] data) {
this.data = new char[data.length];
for (int i = 0; i < data.length; i++) {
this.data[i] = data[i];
}
length = data.length;
}
public SeqString(String str) {
this.data = str.toCharArray();
length = str.length();
}
}
```
替换顺序串中的子串:
```java
public SeqString replace(SeqString s1, SeqString s2) {
SeqString result = new SeqString();
int index = indexOf(s1); // 找到s1在原串中的位置
if (index == -1) {
return this; // 如果原串中没有s1,则直接返回原串
}
int i = 0;
while (i < index) {
result.append(data[i++]); // 将s1之前的部分添加到结果串中
}
i = 0;
while (i < s2.length) {
result.append(s2.charAt(i++)); // 将s2添加到结果串中
}
i = index + s1.length();
while (i < length) {
result.append(data[i++]); // 将s1之后的部分添加到结果串中
}
return result;
}
```
JAVA创建顺序串类SeqString的新建串
以下是JAVA创建顺序串类SeqString的新建串的示例代码:
```
public class SeqString {
private char[] data;
public SeqString() {
this.data = new char[0];
}
public SeqString(String str) {
this.data = str.toCharArray();
}
public SeqString(char[] charArray) {
this.data = charArray.clone();
}
public int length() {
return this.data.length;
}
public char charAt(int index) {
return this.data[index];
}
public SeqString substring(int beginIndex, int endIndex) {
char[] sub = new char[endIndex - beginIndex];
System.arraycopy(this.data, beginIndex, sub, 0, endIndex - beginIndex);
return new SeqString(sub);
}
public SeqString concat(SeqString str) {
char[] newData = new char[this.data.length + str.length()];
System.arraycopy(this.data, 0, newData, 0, this.data.length);
System.arraycopy(str.data, 0, newData, this.data.length, str.length());
return new SeqString(newData);
}
public boolean equals(SeqString str) {
if (this.data.length != str.length()) {
return false;
} else {
for (int i = 0; i < this.data.length; i++) {
if (this.data[i] != str.charAt(i)) {
return false;
}
}
return true;
}
}
public int indexOf(SeqString str) {
for (int i = 0; i <= this.data.length - str.length(); i++) {
if (this.substring(i, i + str.length()).equals(str)) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
SeqString str1 = new SeqString();
SeqString str2 = new SeqString("Hello");
SeqString str3 = new SeqString(new char[] {'W', 'o', 'r', 'l', 'd'});
System.out.println(str1.length()); // 0
System.out.println(str2.charAt(1)); // e
System.out.println(str3.substring(1, 3).equals(new SeqString("or"))); // true
SeqString str4 = str2.concat(str3);
System.out.println(str4.equals(new SeqString("HelloWorld"))); // true
SeqString str5 = new SeqString("Hello");
SeqString str6 = new SeqString("World");
System.out.println(str5.equals(str6)); // false
System.out.println(str4.indexOf(str3)); // 5
}
}
```
在这个示例代码中,我们创建了一个SeqString类,实现了字符串的各种操作,包括新建空串、新建指定字符串的串、新建指定字符数组的串、获取字符串长度、获取指定位置字符、截取子串、串连接、判断相等、查找子串在主串中的位置等方法。在main方法中,我们对这些方法进行了测试。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)
![](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)