字符串转数组工具类

原文 地址:http://qq1988627.iteye.com/blog/1140649




Java代码   收藏代码
  1. package com.byd.mes.util;  
  2.   
  3. /* 
  4.  * To change this template, choose Tools | Templates 
  5.  * and open the template in the editor. 
  6.  */  
  7.   
  8. import java.util.ArrayList;  
  9. import java.util.HashMap;  
  10. import java.util.List;  
  11. import java.util.Map;  
  12.   
  13. /** 
  14.  *字符串转数组的工具类 
  15.  *  
  16.  * @author  */  
  17. public class ArrayUtil {  
  18.   
  19.     /** 
  20.      *将字符串转换成一维数组 
  21.      *  
  22.      * @param input 
  23.      * @param spliter 
  24.      * @return 
  25.      */  
  26.     public static String[] strToArray(String input, String spliter) {  
  27.         if (input.indexOf(spliter) < 0) {  
  28.             return new String[] { input };  
  29.         } else {  
  30.             return input.split(spliter);  
  31.         }  
  32.     }  
  33.   
  34.     /** 
  35.      * 将字符串转成二维数组 
  36.      *  
  37.      * @param input 
  38.      * @param spliter 
  39.      * @return 
  40.      */  
  41.     public static List<Map> strToMapList(String input, String spliter) {  
  42.         List<Map> tempLst = new ArrayList<Map>();  
  43.         if (input.indexOf(spliter) < 0) {  
  44.             return tempLst;  
  45.         } else {  
  46.             String[] temp = input.split(spliter);  
  47.             if (temp.length % 2 != 0) {// 奇数个  
  48.                 return tempLst;  
  49.             } else {// 偶数个  
  50.                 for (int i = 0; i < temp.length / 2; i++) {  
  51.                     Map tempMap = new HashMap();  
  52.                     tempMap.put("X", temp[i * 2]);  
  53.                     tempMap.put("Y", temp[i * 2 + 1]);  
  54.                     tempLst.add(tempMap);  
  55.                 }  
  56.                 return tempLst;  
  57.             }  
  58.         }  
  59.     }  
  60.   
  61.     public static String strArrToString(String[] arrStrs) {  
  62.         StringBuffer sb = new StringBuffer();  
  63.         String ret = "";  
  64.         for (String str : arrStrs) {  
  65.             sb.append(str + ";");  
  66.         }  
  67.         ret = sb.substring(0, sb.length() - 1);  
  68.         return ret;  
  69.     }  
  70. }  

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值