gpt4 book ai didi

java - 如何检测字符串中的字母并切换它们?

转载 作者:行者123 更新时间:2023-12-02 11:17:21 25 4
gpt4 key购买 nike

如何检测字符串中的字母并切换它们?

我想过这样的事情......但这可能吗?

//For example:
String main = hello/bye;

if(main.contains("/")){
//Then switch the letters before "/" with the letters after "/"
}else{
//nothing
}

最佳答案

好吧,如果您对厚脸皮正则表达式感兴趣:P

public static void main(String[] args) {
String s = "hello/bye";
//if(s.contains("/")){ No need to check this
System.out.println(s.replaceAll("(.*?)/(.*)", "$2/$1")); // () is a capturing group. it captures everything inside the braces. $1 and $2 are the captured values. You capture values and then swap them. :P
//}

}

操作:

bye/hello --> This is what you want right?

关于java - 如何检测字符串中的字母并切换它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27923491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com