String类的delete方法怎么删除字符串最后一个字符
时间: 2023-06-16 07:02:17 浏览: 66
String类的delete方法可以通过指定起始位置和结束位置的方式来删除指定的字符序列。如果要删除字符串最后一个字符,可以将起始位置设置为字符串长度减一,将结束位置设置为字符串长度。具体实现代码如下:
```java
String str = "hello world";
str = str.delete(str.length() - 1, str.length());
```
在上面的代码中,首先定义了一个字符串`str`,然后通过`delete`方法将字符串最后一个字符删除,并将删除后的结果重新赋值给`str`。需要注意的是,这里的起始位置应该是字符串长度减一,而不是零。
相关问题
仿照标准字符串类string,设置一个自己的字符串类
好的,我可以为您提供一个简单的自定义字符串类的示例代码:
```cpp
#include <iostream>
#include <cstring>
class MyString {
private:
char* str;
int length;
public:
MyString() {
length = 0;
str = new char[1];
str[0] = '\0';
}
MyString(const char* s) {
length = std::strlen(s);
str = new char[length + 1];
std::strcpy(str, s);
}
MyString(const MyString& other) {
length = other.length;
str = new char[length + 1];
std::strcpy(str, other.str);
}
~MyString() {
delete[] str;
}
MyString& operator=(const MyString& other) {
if (this != &other) {
delete[] str;
length = other.length;
str = new char[length + 1];
std::strcpy(str, other.str);
}
return *this;
}
MyString operator+(const MyString& other) const {
MyString result;
result.length = length + other.length;
delete[] result.str;
result.str = new char[result.length + 1];
std::strcpy(result.str, str);
std::strcat(result.str, other.str);
return result;
}
bool operator==(const MyString& other) const {
return std::strcmp(str, other.str) == 0;
}
bool operator!=(const MyString& other) const {
return !(*this == other);
}
char& operator[](int index) {
return str[index];
}
const char& operator[](int index) const {
return str[index];
}
int size() const {
return length;
}
const char* c_str() const {
return str;
}
friend std::ostream& operator<<(std::ostream& os, const MyString& s) {
os << s.str;
return os;
}
};
int main() {
MyString s1("Hello");
MyString s2("World");
MyString s3 = s1 + s2;
std::cout << s3 << std::endl;
std::cout << s3.size() << std::endl;
std::cout << s3.c_str() << std::endl;
return 0;
}
```
这个自定义字符串类包含了一些常见的操作符重载和成员函数,例如构造函数、析构函数、复制构造函数、赋值运算符、加法运算符、比较运算符、下标运算符、size函数和c_str函数等。
字符串中只删除一个指定字符串
可以通过字符串的replace方法和count方法来实现。先使用count方法判断指定字符串在原字符串中出现的次数,如果出现次数大于等于2,则使用replace方法删除第一个出现的指定字符串。如果只出现了一次,则直接使用replace方法删除该指定字符串即可。
下面是Python代码示例:
```python
def delete_one_string(s, target):
count = s.count(target)
if count >= 2:
return s.replace(target, '', 1)
elif count == 1:
return s.replace(target, '')
else:
return s
```
其中,s为原字符串,target为要删除的指定字符串。如果指定字符串在原字符串中出现了多次,使用replace方法并设置count参数为1可以删除第一个出现的指定字符串。如果指定字符串只出现了一次,则直接使用replace方法删除即可。如果指定字符串没有出现,则直接返回原字符串。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)