String和StringBuffer的幾個小程式
1、//把陣列拼接成一個字串 public class StringBufferDemo3 { public static void main(String[] args) { int[] arr={1,2,3}; String str=""; str=str+"["; String result=""; result=arrayToString(arr); str+=result; System.out.println(str); String str1=""; str1=str1+"["; String result1=""; result1=arrayToString1(arr); str1+=result1; System.out.println(str1); } public static String arrayToString(int [] arr){ String s=""; for(int i=0;i<=arr.length-1;i++){ if (i==arr.length-1){ s+=arr[i]; s+="]"; }else { s += arr[i]; s += ","; } } return s; } public static String arrayToString1(int [] arr){ StringBuffer sb=new StringBuffer(); for(int i=0;i<=arr.length-1;i++){ if (i==arr.length-1){ sb.append(arr[i]).append("]"); }else { sb.append(arr[i]).append(","); } } return sb.toString(); } }
2、//把字串反轉 public class StringBufferDemo4 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("請輸入:"); String str=sc.nextLine(); char[]ch=str.toCharArray(); String result=ResverString(ch); System.out.println(result); String result1=ResverString1(str); System.out.println(result1); } public static String ResverString(char[]ch){ String s=""; for(int i=ch.length-1;i>=0;i--){ s+=ch[i]; } return s; } public static String ResverString1(String s){ StringBuffer sb=new StringBuffer(s); sb.reverse(); return sb.toString(); } }
3、//判斷輸入的字元是否對稱 import java.util.Scanner; public class StringBufferDemo5 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("請輸入:"); String str=sc.nextLine(); boolean result=isSelfSame(str); System.out.println(result); boolean result1=isSelfSame1(str); System.out.println(result1); } public static boolean isSelfSame(String s){ char[] ch=s.toCharArray(); boolean flag=true; for(int start=0,end=ch.length-1;start<=end;start++,end--){ if(ch[start]!=ch[end]){ flag=false; }else{ flag=true; } } return flag; } public static boolean isSelfSame1(String s){ return new StringBuffer(s).reverse().toString().equals(s); } }
相關文章
- String、StringBuilder和StringBufferUI
- String、StringBuffer和StringBuilder類的UI
- 【Java】String、StringBuilder和StringBufferJavaUI
- C# string、stringBuffer和stringBuilderC#UI
- String、StringBuffer、StringBuilder的理解UI
- String、StringBuffer、StringBuilder剖析UI
- String,StringBuilder,StringBuffer的區別UI
- String、StringBuffer、StringBuilder的區別UI
- String、StringBuilder、StringBuffer的區別UI
- String StringBuild StringBuffer的區別UI
- Java入門系列-13-String 和 StringBufferJava
- Java之String和StringBuffer堆疊圖分析Java
- 一篇講清楚String、StringBuffer和StringBuildUI
- Java中的字串操作(比較String,StringBuiler和StringBuffer)Java字串UI
- String,StringBuffer與StringBuilder的區別UI
- String、StringBuffer、StringBuilder區別?UI
- String,StringBuffer,StringBuilder區別UI
- 老生常談 String、StringBuilder、StringBufferUI
- 第二十節:詳細講解String和StringBuffer和StringBuilder的使用UI
- 【資料結構】串(String、StringBuilder、StringBuffer)的JAVA程式碼實現資料結構UIJava
- Android- String、StringBuffer、StringBuilder區別AndroidUI
- java複習之 String,StringBuffer,StringBuilderJavaUI
- StringBuffer 和 StringBuilder 的 3 個區別UI
- 【扯皮系列】一篇與眾不同的 String、StringBuilder 和 StringBuffer 詳解UI
- 你必須搞清楚的String,StringBuilder,StringBufferUI
- 從原始碼看String,StringBuffer,StringBuilder的區別原始碼UI
- 面試必知:String、StringBuilder、StringBuffer的區別面試UI
- Java 之String、StringBuffer 和 StringBuilder 三者區別介紹JavaUI
- String s = new String(" a ") 到底產生幾個物件?物件
- 分享幾個微信小程式開發框架和工具微信小程式框架
- Debug和幾個小例項
- String StringBuffer StringBuilder的區別(前端面試必備)UI前端面試
- Java中的String,StringBuilder,StringBuffer三者的區別JavaUI
- Java容器深入淺出之String、StringBuffer、StringBuilderJavaUI
- Java中String,StringBuffer,StringBuilder基礎知識JavaUI
- 認識Java中String與StringBuffer以及StringBuilderJavaUI
- 小程式測試的幾個小Tips(趕快收藏啦!)
- 開發了幾個小程式後,說說我對小程式的看法