×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

Java TEST!!

Write a program that takes a single string as input, and outputs a list of strings it has split the input string into.


The delimiter for splitting the input string is the special "|" character. If you want a "|" character to appear within an output string and not be interpreted as a splitting character, then it must be preceded by the escape character "\". Any character followed by the "\" character will be interpreted literally, and not treated as a special character. In this case, the "\" escape character is thrown away, and the character following it is appended literally to the current output string. So for example, "a|b" would be split as "a", "b" but "a\|b" would not be split and would appear in the output as a single string "a|b".

For example, the following input string


"abc|def|gh\|ij\\kl\m"

would be split as follows


"abc", "def", "gh|ij\klm"
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / Java TEST!!
    Write a program that takes a single string as input, and outputs a list of strings it has split the input string into.


    The delimiter for splitting the input string is the special "|" character. If you want a "|" character to appear within an output string and not be interpreted as a splitting character, then it must be preceded by the escape character "\". Any character followed by the "\" character will be interpreted literally, and not treated as a special character. In this case, the "\" escape character is thrown away, and the character following it is appended literally to the current output string. So for example, "a|b" would be split as "a", "b" but "a\|b" would not be split and would appear in the output as a single string "a|b".

    For example, the following input string


    "abc|def|gh\|ij\\kl\m"

    would be split as follows


    "abc", "def", "gh|ij\klm"
    • use Regular Expression, here is VB code, you can easily find some libary on the net
      Dim a As RegExp
      Set a = New RegExp

      a.Global = True
      a.Pattern = "\\\\"
      s = "abc\\\\|def|gh\\\\\\\\\\\|ij\\kl\\\\\m"
      b = a.Replace(s, Chr(7))


      a.Pattern = "([^\\])\|"
      b = a.Replace(b, "$1 ")
      a.Pattern = "\\\|"
      b = a.Replace(b, "|")

      a.Pattern = "\\(\w)"
      b = a.Replace(b, "$1")

      a.Pattern = Chr(7)
      b = a.Replace(b, "\")
    • This is a very common tokenize problem in C++ and Java. Take a look at StringTokenizer class, you will get some hints.
    • Just use oroinc 的 perl library . easy .
      • first time heard. Java embeds a Perl Lib?
        whould you give out some sample code for that?

        The generic way to solve that problem is lex/yacc,
        I'm sure there are a lot of Java lex/yacc libs on the net
        • check it out : http://jakarta.apache.org/oro/
    • why it doesn't work normal? please give help
      本文发表在 rolia.net 枫下论坛package com.bgi.util;

      import java.util.*;


      public class Spliter {

      public Spliter() {
      }
      private static String nameOfThisClass = "Spliter";
      public final static String DELIMITER = "|";

      public static String[] split(String s){
      return split(s,DELIMITER);
      }

      public static String[] split(String s, String delimiter){
      checkSource(s);
      int delimiterLength;int stringLength = s.length();
      if (delimiter == null || (delimiterLength = delimiter.length()) == 0){.
      return new String[] {s};
      }
      int count;
      int start;
      int end;
      char slash = '\\';

      // Scan s and count the tokens.
      count = 0;
      start = 0;
      while((end = s.indexOf(delimiter, start)) != -1) {

      if (s.charAt(end-1)!=slash){
      count++;
      start = end + delimiterLength;
      } else {
      start = end + delimiterLength;
      continue;
      }}

      count++;

      String[] result = new String[count];
      count = 0;
      start = 0;
      while((end = s.indexOf(delimiter, start)) != -1) {

      if(s.charAt(end-1)!=slash){
      result[count] = (s.substring(start, end));
      count++;
      start = end + delimiterLength;
      } else {
      start = end + delimiterLength;
      continue;
      }}

      end = stringLength;
      result[count] = s.substring(start, end);

      return (result);
      }

      /**
      * Check if the input parameter is null
      *
      * @throws IllegalArgumentException if source is null
      */
      private static void checkSource(String source)
      throws IllegalArgumentException
      {
      if ( source == null ){
      System.out.println( nameOfThisClass + "The input string is null..");
      throw new IllegalArgumentException();
      }
      }

      /**
      * Test method
      */
      public static void main(String args[]){

      try{
      String a[]=Spliter.split(args[0]);
      int i;
      for (i=0; i<a.length; i++ ){
      System.out.print(a[i]+"^");
      }
      }
      catch(Exception e){
      e.printStackTrace();
      }
      }

      }更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • because you are revamping the wheel. use regular expression.
    • This becomes an interesting question
      I can do it by using StringTokenizer class.

      Let me give you an hint: When you get the token, check the "\" char, if you found one replace it with a "|" char and oncatenate it with next token.
    • 看这儿!!!
      本文发表在 rolia.net 枫下论坛import java.util.*;

      public class TestTokenization {
      private static List tokenize(String str, String dms) {
      ArrayList list = new ArrayList( );
      StringTokenizer tok = new StringTokenizer(str, dms);
      while (tok.hasMoreTokens()) {
      list.add(tok.nextToken());
      }
      return list;
      }

      private static void refresh(List list, int position, String s) {
      if (position >= list.size() ) {
      return;
      }
      String temp = (String) list.get(position);
      if (temp.endsWith(s)) {
      temp = temp.substring(0, temp.indexOf(s))
      + (String) list.get(position + 1);
      list.set(position, temp);
      list.remove(position + 1);
      }
      refresh(list, ++position, s);
      }

      private static void prt(List list) {
      Iterator it = list.iterator();
      while (it.hasNext()) {
      System.out.print(it.next() + " ");
      }
      }
      public static void main(String[] args) {
      String s = "abc\\|ef|gh|lmn\\|\\zx|yw";
      List list = tokenize(s, "|");
      prt(list);
      System.out.println("");
      refresh(list, 0, "\\");
      prt(list);
      }
      }更多精彩文章及讨论,请光临枫下论坛 rolia.net