字符串的字串,字母顺序翻转。

本文介绍了一种处理字符串的方法,包括去除特殊字符并获取所有子串,以及如何实现单词字母顺序的翻转,特别关注末尾标点符号的处理。

String str = "113@ ere qqq yyui",写出这个字符串的子串。

public static void main(String[] args) throws Exception {

        String s
= "113@ ere qqq yyui";
        s
=s.replaceAll("[^a-zA-Z 0-9]", "");
       
for (String str: s.split(" ")) {
            System.out.println(str);
        }
        
        
   编写一个程序,将下面的一段文本中的各个单词的字母顺序翻转,
“To be or not to be",将变成"oT eb ro ton ot eb."。
   

 //末尾标点是不是不要反转?
        s = "To be or not to be.";//"oT eb ro ton ot eb."。
        StringBuffer sBuffer = new StringBuffer();
       
for (String str: s.split("//s")) {
           
char punctuate=' ';
           
char[] chars = str.toCharArray();
           
for (int i = chars.length-1;i>=0;i--) {
               
if (i==chars.length-1 && !Character.isLetter(chars[i])) {
                    punctuate
= chars[i];
                   
continue;
                }
                sBuffer.append(chars[i]);
            }
            sBuffer.append(punctuate);
        }
        System.out.println(sBuffer);
    }

### 字符串获取子串的方法及示代码 在编程中,字符串操作是非常常见的任务之一,其中包括查找子串和获取子串。以下是几种常见编程语言中实现字符串获取子串的方法及示代码。 #### C++ 中的字符串查找子串方法 C++ 提供了标准库函数 `std::string::find` 来查找子串的位置[^1]。如果找到子串,则返回其首次出现的索引;否则返回 `std::string::npos`。 ```cpp #include <iostream> #include <string> int main() { std::string str = "Hello, World!"; std::string substr = "World"; size_t pos = str.find(substr); // 查找子串 if (pos != std::string::npos) { std::cout << "子串 \"" << substr << "\" 在位置 " << pos << " 被找到。" << std::endl; } else { std::cout << "子串未找到。" << std::endl; } return 0; } ``` #### Python 中的字符串切片方法 Python 提供了非常简洁的切片语法来获取子串[^2]。通过指定起始索引、结束索引以及步长,可以灵活地获取字符串的一部分。 ```python s = "Hello, World!" # 获取从索引 0 到索引 5 的子串(不包括索引 5) substring1 = s[0:5] print(substring1) # 输出: "Hello" # 获取从索引 7 开始到末尾的子串 substring2 = s[7:] print(substring2) # 输出: "World!" # 使用负数索引获取子串 substring3 = s[-6:-1] print(substring3) # 输出: "World" ``` #### Python 中的字符串查找方法 除了切片外,Python 还提供了多种内置方法用于查找子串[^4]。如,`str.find()` 和 `str.index()` 方法都可以用来定位子串的位置。 ```python s = "Hello, World!" substr = "World" # 使用 find 方法查找子串 pos1 = s.find(substr) if pos1 != -1: print(f"子串 \"{substr}\" 在位置 {pos1} 被找到。") # 输出: 子串 "World" 在位置 7 被找到。 else: print("子串未找到。") # 使用 index 方法查找子串 try: pos2 = s.index(substr) print(f"子串 \"{substr}\" 在位置 {pos2} 被找到。") # 输出: 子串 "World" 在位置 7 被找到。 except ValueError: print("子串未找到。") ``` #### 其他编程语言中的字符串子串操作 在其他编程语言中,如 Java 和 JavaScript,也有类似的字符串子串操作方法。 - **Java** 中使用 `String.indexOf()` 方法查找子串,并用 `String.substring()` 方法获取子串[^3]。 - **JavaScript** 中使用 `String.indexOf()` 方法查找子串,并用 `String.slice()` 或 `String.substring()` 方法获取子串。 ```java public class Main { public static void main(String[] args) { String str = "Hello, World!"; String substr = "World"; int pos = str.indexOf(substr); // 查找子串 if (pos != -1) { System.out.println("子串 \"" + substr + "\" 在位置 " + pos + " 被找到。"); } else { System.out.println("子串未找到。"); } // 获取子串 String substring = str.substring(7); System.out.println("从索引 7 开始的子串为: " + substring); // 输出: "World!" } } ``` ```javascript let str = "Hello, World!"; let substr = "World"; let pos = str.indexOf(substr); // 查找子串 if (pos !== -1) { console.log(`子串 "${substr}" 在位置 ${pos} 被找到。`); } else { console.log("子串未找到。"); } // 获取子串 let substring = str.slice(7); console.log(`从索引 7 开始的子串为: "${substring}"`); // 输出: "World!" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值