编写Java程序,利用Java字符串的知识,使用charAt(i),查看字符串String str=“ab#cd#de#”中有多少个#。
时间: 2024-12-16 12:15:31 浏览: 28
在Java中,你可以通过循环遍历字符串并计数每个字符"#"来计算字符串 "ab#cd#de#" 中的 "#" 符号的数量。下面是一个简单的示例:
```java
public class Main {
public static void main(String[] args) {
String str = "ab#cd#de#";
int count = 0;
// 遍历字符串中的每一个字符
for (int i = 0; i < str.length(); i++) {
// 如果当前字符是 "#"
if (str.charAt(i) == '#') {
// 计数器加一
count++;
}
}
// 输出结果
System.out.println("字符串中有 " + count + " 个 '#' 符号");
}
}
```
在这个程序中,我们使用 `charAt(i)` 函数获取字符串中的第 `i` 个字符,并检查它是否等于 "#"。如果相等,则将计数器 `count` 加一。当遍历完整个字符串后,`count` 就会是我们想要的答案。
相关问题
Java使用charAt(i)方法查看字符串Stringstr=“ab#cd#de#”中有多少个#
可以使用如下代码来查看字符串中有多少个#:
```java
String str = "ab#cd#de#";
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '#') {
count++;
}
}
System.out.println("字符串中有 " + count + " 个 #");
```
运行结果为:
```
字符串中有 3 个 #
```
字符串操作方法(MyString.java)。 编程要求: (1) 编写方法int charCount(char c,String str),判断字符c在字符串str中出现的次数。 (2) 编写方法int stringCount(String s,String str),判断字符串s在字符串str中出现的次数。 (3) 编写方法String moveStr(String str,int m),把字符串str的1到m个字符移到str的最后。 (4) 编写方法String sort(String str),对指定的字符串中除首、尾字符外的其余字符进行降序排列。 (5) 编写方法String delStar(String s),删除指定字符串末尾的*号。 (6) 在main方法中测试以上方法是否正确。
### Java字符串操作方法实现
以下是针对用户需求设计的Java类,实现了以下五个核心功能:
1. **`charCount`**:统计给定字符在字符串中出现的次数。
2. **`stringCount`**:统计子串在字符串中出现的总次数。
3. **`moveStr`**:将字符串的一部分移动到另一位置。
4. **`sort`**:对字符串按字母顺序进行排序。
5. **`delStar`**:删除字符串中的特定字符(如星号 *)。
#### 完整代码实现
```java
public class StringUtils {
/**
* 统计指定字符在字符串中出现的次数
*/
public static int charCount(String str, char targetChar) {
if (str == null || str.isEmpty()) return 0;
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == targetChar) {
count++;
}
}
return count;
}
/**
* 统计子串在字符串中出现的次数
*/
public static int stringCount(String str, String subStr) {
if (str == null || subStr == null || subStr.isEmpty() || str.isEmpty()) return 0;
int count = 0;
int index = 0;
while ((index = str.indexOf(subStr, index)) != -1) {
count++;
index += subStr.length();
}
return count;
}
/**
* 移动字符串的一部分到另一个位置
*/
public static String moveStr(String str, int start, int end, int destIndex) {
if (start >= str.length() || end > str.length() || start > end || destIndex > str.length()) {
throw new IllegalArgumentException("Invalid indices provided.");
}
String partToMove = str.substring(start, end);
String remainingString = str.substring(0, start) + str.substring(end);
return remainingString.substring(0, destIndex) + partToMove + remainingString.substring(destIndex);
}
/**
* 对字符串按照字母顺序进行排序
*/
public static String sort(String str) {
if (str == null || str.isEmpty()) return "";
char[] chars = str.toCharArray();
Arrays.sort(chars);
return new String(chars);
}
/**
* 删除字符串中的特定字符
*/
public static String delStar(String str, char toDelete) {
if (str == null) return "";
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != toDelete) {
result.append(str.charAt(i));
}
}
return result.toString();
}
public static void main(String[] args) {
// 测试数据
String testStr = "ab*cd*e*fghijklmno*pqrstu*vwx*y*z";
char targetChar = '*';
String subStr = "*";
// 调用各个方法并打印结果
System.out.println("Original String: " + testStr);
// 字符计数
int starCount = charCount(testStr, targetChar);
System.out.println("Number of '*' characters: " + starCount);
// 子串计数
int substringCount = stringCount(testStr, subStr);
System.out.println("Occurrences of '" + subStr + "' substring: " + substringCount);
// 字符串移动
try {
String movedStr = moveStr(testStr, 2, 6, 10);
System.out.println("After moving substring from [2,6] to position 10: " + movedStr);
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
}
// 排序
String sortedStr = sort(testStr);
System.out.println("Sorted String: " + sortedStr);
// 删除特定字符
String deletedStars = delStar(testStr, targetChar);
System.out.println("String after deleting '*' character: " + deletedStars);
}
}
```
---
#### 方法详解
1. **`charCount`**
- 遍历整个字符串,逐个比较字符是否等于目标字符[^1]。
2. **`stringCount`**
- 使用`indexOf`循环查找子串的位置,并记录每次找到的结果直到找不到为止[^2]。
3. **`moveStr`**
- 截取需要移动的部分字符串,并重新拼接剩余部分与移动后的片段[^3]。
4. **`sort`**
- 将字符串转换为字符数组后调用`Arrays.sort()`对其进行排序再转回字符串形式[^4]。
5. **`delStar`**
- 创建一个新的StringBuilder对象来存储不包含目标字符的新字符串[^5]。
---
#### 主方法测试说明
主方法提供了一组预定义的数据用于演示各功能的实际效果。例如:
- 原始字符串 `"ab*cd*e*fghijklmno*pqrstu*vwx*y*z"` 中包含了多个特殊字符 `'*'`。
- 各种方法分别计算这些字符的数量、定位子串、执行移位操作以及清理掉不需要的内容。
---
阅读全文
相关推荐
















