`
doll
  • 浏览: 82274 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • J2ME
阅读更多
我也不知道是因为什么原因 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;
		}
	}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics