Java實現(xiàn)字符串反轉(zhuǎn)
更新時間:2022年04月14日 14:02:46 作者:農(nóng)碼一生
這篇文章介紹了Java實現(xiàn)字符串反轉(zhuǎn)的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
第一種:
public class Main {
public static void main(String[] args) {
String s1 = "asdfghjkl";
System.out.println(new StringBuilder(s1).reverse().toString());
}
}第二種:
public class Main {
public static void main(String[] args) {
String s1 = "asdfghjkl";
String[] s = s1.split("");
List<String> list = list = Arrays.asList(s);
Collections.reverse(list);
System.out.println(list);
}
}第三種:
public class Main {
public static void main(String[] args) {
String s1 = "asdfghjkl"; System.out.println(new Main().swapWords(s1));
}
public void swap(char[] arr, int begin, int end) {
while (begin < end) {
char temp = arr[begin];
arr[begin] = arr[end];
arr[end] = temp;
begin++;
end--;
}
}
public String swapWords(String str) {
char[] arr = str.toCharArray();
swap(arr, 0, arr.length - 1);
int begin = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] == ' ') {
swap(arr, begin, i - 1);
begin = i + 1;
}
}
return new String(arr);
}
}到此這篇關(guān)于Java實現(xiàn)字符串反轉(zhuǎn)的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
完美解決IDEA Ctrl+Shift+f快捷鍵突然無效的問題
這篇文章主要介紹了完美解決IDEA Ctrl+Shift+f快捷鍵突然無效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
一文快速了解spring?boot中的@idempotent注解
idempotence注解是RESTful API設(shè)計中一個重要的概念,它可以保證操作的可靠性和一致性,下面這篇文章主要給大家介紹了關(guān)于spring?boot中@idempotent注解的相關(guān)資料,需要的朋友可以參考下2024-01-01
詳解Spring?Security?捕獲?filter?層面異常返回我們自定義的內(nèi)容
Spring?的異常會轉(zhuǎn)發(fā)到?BasicErrorController?中進行異常寫入,然后才會返回客戶端。所以,我們可以在?BasicErrorController?對?filter異常進行捕獲并處理,下面通過本文給大家介紹Spring?Security?捕獲?filter?層面異常,返回我們自定義的內(nèi)容,感興趣的朋友一起看看吧2022-05-05
mybatis模糊查詢之bind標簽和concat函數(shù)用法詳解
大家都知道bind 標簽可以使用 OGNL 表達式創(chuàng)建一個變量井將其綁定到上下文中,接下來通過本文給大家介紹了mybatis模糊查詢——bind標簽和concat函數(shù)用法,需要的朋友可以參考下2022-08-08
關(guān)于Elasticsearch封裝公共索引增刪改查
索引是Elasticsearch中存儲數(shù)據(jù)的邏輯單元,類似于關(guān)系數(shù)據(jù)庫中的表,它包含多個文檔,每個文檔都是一個結(jié)構(gòu)化的JSON數(shù)據(jù)格式,在實際應用中,索引的使用與配置可以依據(jù)不同的方案進行,例如在Spring Boot項目中,可以選擇自動配置或者手動編寫配置類2024-10-10

