J2ME平台上的字符串的切割方法 split()

我也不知道是因为什么原因 J2ME 上字符串处理方法中没有 split()这个方法;
有时候要做字符串切割的时候 很麻烦 下面我给出一个以前老师教我的mySplit方法 跟split()方法用法相似。我就懒的写了 直接copy

/**
* 切割字符串
*
* @param str
* 要切割的字符串
* @param chr
* @return
*/
public static String[] mySplict(String str, char chr) {
/**
* 返回的字符串
*/
String[] data = null;
try {
// a|b|C|d
// vector性能很低,用System.arraycopy来代替vector;上网查System.arraycopy的使用方法和优点
// 放的是字符chr的位置
Vector vector = new Vector();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (chr == c) {
// i是字符的位置
vector.addElement(new Integer(i));
}
}

// 字符串中没有要切割的字符
if (vector.size() == 0) {
data = new String[] { str };
}

if (vector.size() >= 1) {
data = new String[vector.size() + 1];
}
for (int i = 0; i < vector.size(); i++) {
/**
* 位置
*/
int index = ((Integer) vector.elementAt(i)).intValue();
String temp = "";
if (i == 0)// 第一个#
{
if (vector.size() == 1) {
temp = str.substring(index + 1);
data[1] = temp;
}
temp = str.substring(0, index);
data[0] = temp;
} else if (i == vector.size() - 1)// //最后一个#
{
int preIndex = ((Integer) vector.elementAt(i - 1))
.intValue();
temp = str.substring(preIndex + 1, index);// 最后一个#前面的内容
data[i] = temp;
temp = str.substring(index + 1);// 最后一个#后面的内容
data[i + 1] = temp;
} else {
int preIndex = ((Integer) vector.elementAt(i - 1))
.intValue();
temp = str.substring(preIndex + 1, index);// 最后一个#前面的内容
data[i] = temp;
}

}

} catch (Exception e) {

e.printStackTrace();

} finally {
return data;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值