> Java > java지도 시간 > 본문

Java의 StringTokenizer

WBOY
풀어 주다: 2024-08-30 15:13:21
원래의
821명이 탐색했습니다.

다음 문서에서는 Java의 StringTokenizer에 대한 개요를 제공합니다. Java의 String Tokenizer를 사용하면 애플리케이션이 특정 문자열을 일부 구분 기호를 기반으로 토큰으로 나눌 수 있습니다. 문자열의 각 분할 부분을 토큰이라고 합니다. 문자열 토크나이저는 내부적으로 String 클래스의 하위 문자열 메서드를 사용하여 토큰을 생성합니다. 문자열 토크나이저는 내부적으로 마지막 토큰의 인덱스를 유지하고 이 인덱스를 기반으로 다음 토큰을 계산합니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

이 글에서는 문자열 토크나이저 클래스에서 사용할 수 있는 다양한 생성자에 대한 자세한 설명을 살펴보겠습니다. 또한 문자열 토크나이저 인스턴스의 생성과 그 안에서 사용 가능한 다양한 메소드의 사용법을 보여주는 Java 코드 예제가 있습니다.

다음은 Java의 문자열 토크나이저 선언입니다.

public class StringTokenizer extends Object
implements Enumeration<Object>
로그인 후 복사

문자열 토크나이저 생성자

String Tokenizer는 레거시 프레임워크의 일부입니다.

다음은 문자열 토크나이저 클래스의 주요 생성자입니다.

  • StringTokenizer(String str): 지정된 문자열에 대한 문자열 토크나이저를 생성합니다. 공백, 탭, 줄 바꿈, 캐리지 리턴 문자 및 용지 공급 문자가 될 수 있는 기본 구분 기호를 설정합니다.
  • StringTokenizer(String str, String delim): 지정된 문자열에 대한 문자열 토크나이저를 생성하며, 지정된 구분 기호를 기반으로 토큰이 생성됩니다.
  • StringTokenizer(String str, String delim, boolean returndelims): 이것은 지정된 문자열에 대한 문자열 토크나이저를 생성하며 토큰은 지정된 구분 기호를 기반으로 생성됩니다. 세 번째 매개변수는 토큰에 구분 기호가 필요한지 여부를 지정하는 부울 값입니다.

위에서 지정한 생성자는 요구사항에 따라 사용할 수 있습니다.

Java에서 문자열 토크나이저의 중요한 방법

Method Name Description
boolean hasMoreTokens() This method checks whether there are more tokens available.
String nextToken() This method returns the value of the next available token.
String nextToken(String delim) This method returns the value of the next available token based on provided delimiter.
boolean hasMoreElements() This works similarly to hasMoreTokens.
Object nextElement() This method is the same as nextToken but returns an object.
int countTokens() This method returns a number of tokens.
메서드 이름 설명 부울 hasMoreTokens() 이 방법은 사용 가능한 토큰이 더 있는지 확인합니다. 문자열 nextToken() 이 메소드는 사용 가능한 다음 토큰의 값을 반환합니다. 문자열 nextToken(문자열 delim) 이 메소드는 제공된 구분 기호를 기반으로 사용 가능한 다음 토큰의 값을 반환합니다. 부울 hasMoreElements() hasMoreTokens와 유사하게 작동합니다. 객체 nextElement() 이 메소드는 nextToken과 동일하지만 객체를 반환합니다. int countTokens() 이 메소드는 다수의 토큰을 반환합니다.

Examples

Given below are the examples:

Example #1

Let us see an example of a string tokenizer class showing the use of the first constructor.

Code:

package com.edubca.stringtokenizerdemo;
import java.util.*;
public class StringTokenizerDemo{
public static void main(String args[]){
//create string tokenizer instance
StringTokenizer st1 =
new StringTokenizer("This is Edubca Java Training");
System.out.println("Tokens separated by space are : ");
while (st1.hasMoreTokens())
System.out.println(st1.nextToken());
}
} <strong> Output:</strong>
로그인 후 복사

Java의 StringTokenizer

Example #2

 In this example, we will see the use of the second constructor of a string tokenizer class that accepts a string and delimiter.

Code:

package com.edubca.stringtokenizerdemo;
import java.util.*;
public class StringTokenizerDemo{
public static void main(String args[]){
//create string tokenizer instance
StringTokenizer st1 =
new StringTokenizer("This,is,Edubca,Java,Training", ",");
System.out.println("Tokens separated by comma are : ");
while (st1.hasMoreTokens())
System.out.println(st1.nextToken());
}
}
로그인 후 복사

Output:

In the above example, we have seen how to create tokens based on a given delimiter in the string tokenizer.

 Java의 StringTokenizer

Example #3

In this example, we will see the use of the third constructor of a string tokenizer class that accepts a string, delimiter and boolean value.

Code:

package com.edubca.stringtokenizerdemo;
import java.util.*;
public class StringTokenizerDemo{
public static void main(String args[]){
//create string tokenizer instance
StringTokenizer st1 =
new StringTokenizer("This,is,Edubca,Java,Training", ",",true);
System.out.println("Tokens separated by comma are : ");
while (st1.hasMoreTokens())
System.out.println(st1.nextToken());
}
}
로그인 후 복사

Output:

 Java의 StringTokenizer

As we can see in the above output delimiter is also considered as a token.

Example #4

In this example, we will how to handle multiple delimiters in java string tokenizer.

Code:

package com.edubca.stringtokenizerdemo;
import java.util.*;
public class StringTokenizerDemo{
public static void main(String args[]){
String stringvalue = "http://127.0.0.1:8080/";
//create string tokenizer instance with multiple delimiters
StringTokenizer st1=new StringTokenizer (stringvalue,"://.");
System.out.println("Tokens generated are : ");
while (st1.hasMoreTokens())
System.out.println(st1.nextToken());
}
}
로그인 후 복사

Here is the output produced after running the above code:

Java의 StringTokenizer

The above tokens are generated by tokenizing strings based on multiple tokens (://.).

Example #5

In this example, we will see the use of the count tokens method in the string tokenizer.

Code:

package com.edubca.stringtokenizerdemo;
import java.util.*;
public class StringTokenizerDemo{
public static void main(String args[]){
//create string tokenizer instance
StringTokenizer st1 = new StringTokenizer("This,is,Edubca,Java,Training", ",",true);
System.out.println("Number of available tokens are : " + st1.countTokens());
System.out.println("Tokens separated by comma are : ");
while (st1.hasMoreTokens())
System.out.println(st1.nextToken());
}
}
로그인 후 복사

Output:

Java의 StringTokenizer

Conclusion – StringTokenizer in Java

From the above discussion, we have a clear understanding of what is string tokenizer in java, how it’s created, and what are the different methods available in the string tokenizer class.

위 내용은 Java의 StringTokenizer의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!