首页 > 代码库 > 字符串和整形数组的相互转化(JAVA程序)

字符串和整形数组的相互转化(JAVA程序)

 1 package te; 2 public class StringConvert { 3     static int[] a = {0,1,1,0,1,1,0,2}; 4     static String s = "0011223344"; 5     public static void main(String[] args) { 6         StringConvert sc = new StringConvert(); 7         System.out.println(sc.intArray2Str(a)); 8         int[] b = sc.str2IntArray(s); 9         for(int i=0; i<b.length; i++) {10             System.out.print(b[i]);11         }    12     }13     14     String intArray2Str(int[] a) {15         16         int len = a.length;17         String str="";18         for(int i=0;i<len; i++) {19             str+=String.valueOf(a[i]);20         }        21         return str;    22     }23     24     int[] str2IntArray(String str) {25         int len = str.length();26         int[] a = new int[len];27         char[] c = str.toCharArray();28         for(int i=0; i<len; i++) {29             a[i] = c[i]-‘0‘;30         }    31         return a;    32     }33 34 }


结果

011011020011223344

 

 1 package te; 2  3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5  6 public class StringConvert { 7     static int[] a = {0,1,1,0,1,1,0,2}; 8     static String s = "0011223340004"; 9     public static void main(String[] args) {10         StringConvert sc = new StringConvert();11         System.out.println(sc.intArray2Str(a));12         int[] b = sc.str2IntArray(s);13         for(int i=0; i<b.length; i++) {14             System.out.print(b[i]);15         }16         System.out.print(‘\n‘);17         sc.indexsof(s);18     }19     20     String intArray2Str(int[] a) {21         22         int len = a.length;23         String str="";24         for(int i=0;i<len; i++) {25             str+=String.valueOf(a[i]);26         }        27         return str;    28     }29     30     int[] str2IntArray(String str) {31         int len = str.length();32         int[] a = new int[len];33         char[] c = str.toCharArray();34         for(int i=0; i<len; i++) {35             a[i] = c[i]-‘0‘;36         }    37         return a;    38     }39     int[] indexsof(String s) {40         41          Pattern p = Pattern.compile("[0]");42          Matcher m = p.matcher(s);43          while(m.find()) {44              System.out.println(m.start());45          }46 47         return null;48         49     }50 }

从一个字符串中找到符合要求字符串的所有位置

字符串和整形数组的相互转化(JAVA程序)