Topic: 怎样用空行来分割文本?

  Print this page

1.怎样用空行来分割文本? Copy to clipboard
Posted by: from_cjsdn
Posted on: 2004-07-16 10:33

12345677890

1234567890

1231111111111

是个文件里的文本

如果把他读入String s分成3个字符串

s.split("xxxx");

xxxx用什么呢?

2.Re:怎样用空行来分割文本? [Re: from_cjsdn] Copy to clipboard
Posted by: wmgreat
Posted on: 2004-07-17 22:50

readline方法

3.Re:怎样用空行来分割文本? [Re: from_cjsdn] Copy to clipboard
Posted by: alin_ass
Posted on: 2004-07-18 00:22

s.split("\n");

也可

4.Re:怎样用空行来分割文本? [Re: from_cjsdn] Copy to clipboard
Posted by: alin_ass
Posted on: 2004-07-18 00:31

我觉得单个字符这样蛮好的,比较快

/*
* Created on 2004-7-17
*/
package org.hziee.netmonitor.util;

import java.util.*;
/**
* @author alin
*/
public class StringUtil {
  /**
   * <p>Splits the provided text into an array, separator specified.
   * This is an alternative to using StringTokenizer.</p>
   *
   * <p>The separator is not included in the returned String array.
   * Adjacent separators are treated as one separator.</p>
   *
   * <p>A <code>null</code> input String returns <code>null</code>.</p>
   *
   * <pre>
   * StringUtils.split(null, *) = null
   * StringUtils.split("", *) = []
   * StringUtils.split("a.b.c", '.') = ["a", "b", "c"]
   * StringUtils.split("a..b.c", '.') = ["a", "b", "c"]
   * StringUtils.split("a:b:c", '.') = ["a:b:c"]
   * StringUtils.split("a b c", ' ') = ["a", "b", "c"]
   * </pre>
   *
   * @param str the String to parse, may be null
   * @param separatorChar the character used as the delimiter,
   * <code>null</code> splits on whitespace
   * @return an array of parsed Strings, <code>null</code> if null String input
   */
  public static String[] split(String str, char separateChar) {
    if (str == null) {
      return null;
    }
    int len = str.length();
    if (len == 0) {
      return new String[0];
    }
    List list = new ArrayList();
    int i = 0, start = 0;
    boolean match = false;
    while (i < len) {
      if (str.charAtLight Bulb == separateChar) {
        if (match) {
          list.add(str.substring(start, i));
          match = false;
        }
        start = ++i;
        continue;
      }
      match = true;
      i++;
    }
    if (match) {
      list.add(str.substring(start, i));
    }
    return (String[]) list.toArray(new String[list.size()]);
  }
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923