2011-11-22 4 views
1

전자 메일 주소를 토큰으로 읽으려면 어떻게합니까? 토큰으로 전자 메일 주소 읽기

내가 토크 나이 방법은 길이가 16 비트의 한계가 있다고보고, 잘 내 토큰은 다음과 같이이다 :

command [email protected] 50 

내가 (이메일 주소가 될 수 있습니다) 이메일을 저장할 수 싶어 번호 (5-1500까지 다양 할 수 있음). 나는 명령 토큰에 신경 쓰지 않는다.

내 코드는 다음과 같습니다

String test2 = command.substring(7); 
StringTokenizer st = new StringTokenizer(test2); 
String email = st.nextToken(); 
String amount = st.nextToken(); 

답변

0

그래서 당신은 당신이 간단하게 할 수 command라는 변수에 데이터가있는 경우 :

String[] tokens = command.split("\w"); //this splits on any whitespace, not just the space 
String email = tokens[1]; 
String amount = tokens[2]; 
:

StringTokenizer st = new StringTokenizer(command); 
st.nextToken(); //discard the "command" token since you don't care about it 
String email = st.nextToken(); 
String amount = st.nextToken(); 

다른 방법으로, 배열에로드 할 문자열의 "분할"를 사용할 수 있습니다

1

당신은 구분 기호로 공간을 사용하는 경우, 왜 이런 식으로 코드가 아닙니다 :

String[] temp =command.split(" "); 
String email = temp[1]; 
String amount = temp[2]; 
2

StringTokenizer 여기에 작업을위한 도구가 아닙니다.

"foo bar"@example.com 

대신 파서 생성기를 사용하여 로컬 부분이 하나의 토큰으로 인용 된 문자열이 곳은 유효한 이메일 주소를 처리 할 수있을 것되지 않기 때문에 이메일은 그냥 처리하기에 너무 복잡하다. 많은 사람들이 완벽하게 좋은 RFC 2822 문법을 가지고 있습니다.

예를 들어, http://users.erols.com/blilly/mparse/rfc2822grammar_simplified.txt은 원하는 제작물 인 addr-spec을 정의하며 명령, 공간, addr-spec, 공백, 숫자에 대한 문법적 제작을 정의한 다음 최상위 레벨의 생산을 일련의 분리 된 것으로 정의 할 수 있습니다 줄 바꿈으로.

0

이메일 주소가 이미 email 변수에 저장되어있는 것처럼 보입니다.

package com.so; 

import java.util.StringTokenizer; 

public class Q8228124 { 
    public static void main(String... args) { 
     String input = "command [email protected] 50"; 

     StringTokenizer tokens = new StringTokenizer(input); 

     System.out.println(tokens.countTokens()); 

     // Your code starts here. 
     String test2 = input.substring(7); 
     StringTokenizer st = new StringTokenizer(test2); 
     String email = st.nextToken(); 
     String amount = st.nextToken(); 

     System.out.println(email); 
     System.out.println(amount); 
    } 
} 

$ java com.so.Q8228124 
3 
[email protected] 
50