메뉴 건너뛰기

tnt_lang

jsp/java 수정된 StringTokenizer

박상현 2001.12.17 15:08 조회 수 : 2518 추천:22

StringTokenizer사용시에
";;;;a;a;" 를 token 해보면 공백문자가 무시되는 단점때문에 만들어 보았습니다.
위의 스트링을 countTokens()해보면 2 라고 나오고 공백들이 무시되잖아요.
제대로 7이라고 반환하도록 만들어 보았습니다.


소스는 jboss 의 샘플들 중에서 String[]을 반환하는 tokenizerWithBlanks라는 메쏘드를
이용해서 StringTokenizer와 유사한 인터페이스의 클래스를 만들어 보았는데요,
원본은 누가 만들었었는지는 알수가 없네요.


import java.util.ArrayList;
import java.util.NoSuchElementException;

/**
* This simple class has one method that tokenizes a string, but allows tokens
* to be blanks. This is in contrast to the java.util.StringTokenizer class
* that assumes that multiple delimiters are a single delimiter. This class is
* designed to tokenize the output of spreadsheets, where not all cells have
* entries
*/

/**
* @(#) Tokenizer.java
* @version 1.0
* @author  Edited by Woo-Chan Cho, number501@yahoo.com.
*/

class  Tokenizer
{
        protected Object[] tokened_array=null;

        protected int token_count=0;

        /**
         * Constructor
         * @param        string         -a string to be parsed.
         * @param     delimi        -the delimiters.
         */
        public Tokenizer(String string, String delim)
        {
                tokenizerWithBlanks(string, delim);
        }
        /**
        * Calculates the number of times that this tokenizer's
        * nextToken method can be called
        * @return        tokend string
        */
        public int countTokens()
        {
                return tokened_array.length-token_count;
        }
        /**
         * Tests if there are more tokens available from this tokenizer's string.
         * If this method returns true, then a subsequent call to nextToken with
         * no argument will successfully return a token.
         * @return         true if and only if there is at least one token in the string after
         *                         the current position; false otherwise.
         */
        public boolean hasMoreElements()
        {
                return hasMoreTokens();
        }
        /**
         * Tests if there are more tokens available from this tokenizer's string.
         * If this method returns true, then a subsequent call to nextToken with
         * no argument will successfully return a token.
         * @return         true if and only if there is at least one token in the string after
         *                         the current position; false otherwise.
         */
        public boolean hasMoreTokens()
        {
                if(tokened_array.length>token_count)
                        return true;
                return false;
        }
        /**
        * Returns the next token from this string tokenizer.
        *
        * @return        tokend string
        * @exception NoSUchElementException
        *                        if there are no more tokens available from this tokenizer's string.
        */

        public String nextToken() throws NoSuchElementException
        {
                if(token_count>=tokened_array.length)
                        throw new NoSuchElementException("Hey, I have no more element.");
                token_count++;
                return tokened_array[token_count-1].toString();
        }

        protected void tokenizerWithBlanks (String input, String delimiter)
        {
                ArrayList array = new ArrayList();
                String token;
                int pos;
                do{
                        pos = input.indexOf(delimiter);
                           if (pos >= 0)
                        {
                                token = input.substring(0, pos);
                                input = input.substring(pos + 1);
                        }
                        else
                        {
                                token = input;
                                input = "";
                        }
                        array.add(token);
                }while (pos >= 0);
                tokened_array = array.toArray();
        }
}
번호 제목 글쓴이 날짜 조회 수
21 JAVA - 한글 인코딩 변환 체크 한방에 끝내기 총관리자 2014.06.07 844
20 clshoesfashionc4u nacyrobert 2013.03.15 1790
19 JMSN messenger-한글지원(2/2) file 박상현 2003.12.16 2226
18 JMSN messenger-한글지원(1/2) file 박상현 2003.12.16 2303
17 ty*bizware for java sample file 운영자 2003.04.02 2284
16 자바교재소스 운영자 2003.01.29 2269
15 PDFBox 0.6.1 - Java PDF Library 운영자 2003.04.15 4234
14 [struts]폼빈에 배열을 사용하기 박상현 2006.05.20 2312
13 클래스 패스와 관련한 문제는 요 jsp 하나로.. 해결 끝이네요.. 하늘과컴 2005.11.15 2228
12 weblogic8.1과 eclipse3.0RC2, LombozRC1용을 이용한 EJB개발 박상현 2004.06.22 2485
11 weblogic5.1과 ant를 이용하여 EJB개발(내부 개발용) 박상현 2004.06.22 5264
10 orion와 eclipse을 이용하여 EJB개발시 참고(내부개발용) 박상현 2004.06.22 2385
9 한글처리 방법/절차 이해 박상현 2003.10.20 1873
8 정수값을 3자리수마다 컴마를 찍기 박상현 2003.10.13 2121
7 시간안에 응답하지 않는함수는 에러(혹은 exception)처리 박상현 2003.10.08 1966
6 batch 작업 박상현 2002.02.13 1982
» 수정된 StringTokenizer 박상현 2001.12.17 2518
4 java에서 system의 property확인 jsp파일 박상현 2001.10.27 2279
3 프리페어스테이트먼트에 ? 표 자리에 값을 셋팅후 만들어진 SQL 문을 보는 유틸 운영자 2003.09.18 3244
2 RAS암호 시스템의 구현 박상현 2001.10.16 3176
위로