String类常用方法总结

本文详细介绍Java中字符串的各种操作方法,包括获取长度、查询字符位置、判断条件、替换内容、转换大小写、截取子串等实用技巧,并涵盖数组转换及分割等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

获取、查询相关

 

获取字符串长度

public int length()

 

根据下标获取字符(获取字符串中指定位置的字符)

public char charAt(int index)

 

查询字符所在位置

public int indexOf(String str)

public int indexOf(String str, int fromIndex)

public int lastIndexOf(String str)

public int lastIndexOf(String str, int fromIndex)

 

判断相关

 

是否包含指定的内容

public boolean contains(CharSequence s)

public int indexOf(String str)

 

是否为空

public boolean isEmpty()

public int length()

 

是否是以指定内容开头

public boolean startsWith(String prefix)

public boolean startsWith(String prefix, int toffset)

 

是否是以指定内容结尾

public boolean endsWith(String suffix)

 

是否内容相同

public boolean equals(Object anObject)

public boolean equalsIgnoreCase(String anotherString)

 

改变内容相关

 

替换

public String replace(CharSequence target, CharSequence replacement)

public String replaceFirst(String regex, String replacement)

 

转换大小写

public String toUpperCase()

public String toLowerCase()

 

去空格

public String trim()

 

截取

public String substring(int beginIndex)

public String substring(int beginIndex, int endIndex)

 

数组相关

 

转换成字节数组

public byte[] getBytes()

 

转换成字符数组

public char[] toCharArray()

 

分割为String[]

public String[] split(String regex)

### C++ STL String 常用方法总结 以下是关于 `std::string` 的一些常见操作及其功能说明: #### 1. 构造字符串对象 可以通过多种方式创建 `std::string` 对象,例如默认构造、初始化列表或者拷贝构造等方式[^3]。 ```cpp #include <iostream> #include <string> int main() { std::string str1; // 默认构造 std::string str2("hello world");// 初始化为指定字符序列 std::cout << str1 << std::endl; std::cout << str2 << std::endl; return 0; } ``` #### 2. 字符串大小与容量 可以使用成员函数获取字符串长度以及调整其容量。 - **size()**: 返回当前存储的字符数。 - **length()**: 同 size(), 表示有效字符数量。 - **capacity()**: 获取分配的空间大小。 - **resize(n)**: 调整字符串的有效长度至 n。 - **reserve(n)**: 预留至少能容纳 n 个字符的空间。 ```cpp #include <iostream> #include <string> int main() { std::string s = "abcdef"; std::cout << "Size: " << s.size() << ", Capacity: " << s.capacity() << std::endl; s.resize(10, 'z'); // 扩展到 10 个字符并填充 'z' std::cout << "Resized: " << s << std::endl; s.reserve(50); // 提前预留空间 std::cout << "Capacity after reserve: " << s.capacity() << std::endl; return 0; } ``` #### 3. 修改字符串内容 提供了一系列用于修改字符串的方法,比如替换子串、追加新数据等。 - **append(str)** 或者运算符 += : 将另一个字符串附加到现有字符串后面。 - **insert(pos, str)**: 插入一段新的文本到特定位置 pos 处。 - **erase(start_pos, num_chars)**: 删除从 start_pos 开始的 num_chars 个字符。 - **replace(start_pos, num_chars, new_str)**: 替代部分区域的内容。 ```cpp #include <iostream> #include <string> int main() { std::string s = "Hello"; s.append(", World!"); // 添加额外的部分 std::cout << s << std::endl; s.insert(5, " there"); // 在索引 5 处插入文字 std::cout << s << std::endl; s.erase(5, 6); // 移除中间的一段话 std::cout << s << std::endl; s.replace(6, 7, "Universe"); // 更改某些词语 std::cout << s << std::endl; return 0; } ``` #### 4. 查找和比较 支持查找某个子串的位置以及执行各种形式的对比测试。 - **find(substr)** 和 **rfind(substr)**: 寻找第一次/最后一次出现 substr 的地方;如果找不到则返回 `std::string::npos`. - **compare(otherStr)**: 判断两个字符串之间的字典序关系 (-1表示小于,0相等,+1大于). ```cpp #include <iostream> #include <string> int main() { std::string s = "This is a sample sentence."; auto idx = s.find("sample"); if (idx != std::string::npos){ std::cout << "'sample' found at index: " << idx << std::endl; } bool isEqual = !s.compare("Another string."); std::cout << "Strings are equal? " << isEqual << std::endl; return 0; } ``` #### 5. 字母转换及其他辅助工具 还包含了针对单个字母的操作手段,如转成大写或小写字母等功能[^1]. - **tolower(c)/toupper(c)**: 把给定字符 c 变更为对应的小写 / 大写版本. ```cpp #include <iostream> #include <string> #include <cctype> int main(){ std::string s="ABCDEFG"; for(int i=0;i<s.size();i++) { s[i]=static_cast<char>(tolower(static_cast<unsigned char>(s[i]))); } std::cout<<s<<std::endl; return 0; } ``` 通过上述这些基本技巧的应用,能够极大简化日常开发中的字符串处理工作量。同时得益于标准模板库的强大特性,开发者无需手动管理内存即可轻松实现复杂逻辑[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值