2011-10-19 2 views
-1

달이 있는데 12과 같은 값이 들어 있습니다. 예를 들어 두 개의 다른 문자열로 분할하려고합니다. a = 1 및 b = 2이다. 어떻게해야합니까?두 자리를 두 개의 다른 문자열로 나누려면 어떻게해야합니까?

+0

월 값은 어떤 유형으로 시작해야합니까? 'int'? – fredley

+0

왜 이것을하고 싶습니까? –

+0

날짜 DATE_OF_REPORT = rs1.getDate ("DATE_OF_REPORT"); \t \t DateFormat month = new SimpleDateFormat ("MM"); \t \t DateFormat 날짜 = 새 SimpleDateFormat ("dd"); \t \t DateFormat year = new SimpleDateFormat ("yy"); – user935988

답변

1

이렇게하는 방법에는 여러 가지가 있습니다.

// Working with Strings ------ 
String str = "12"; 

// Get char array 
char[] chars = str.toCharArray(); 

// Two substrings 
String firstStr = str.substring(0,1); 
String secondStr = str.substring(1,2); 

// Working with ints --------- 
int i = 12; 

int firstInt = i/10; // Divide 
int secondInt = i % 10; // Modulo 
+0

이렇게하면 firstSter = j 및 secondStr = a와 같이 출력됩니다. 숫자 1과 2가 필요합니다. – user935988

+0

날짜 DATE_OF_REPORT = rs1.getDate ("DATE_OF_REPORT"); DateFormat month = 새로운 SimpleDateFormat ("MM"); – user935988

0

사용 String.charAt(index) 방법은 문자를 반환하고 String로 변환 Character.toString(char)를 사용합니다.

+0

오류 또는 잘못된 출력을 제공하지 않는 작업 DATE DATE_OF_REPORT = rs1.getDate ("DATE_OF_REPORT"); DateFormat month = 새로운 SimpleDateFormat ("MM"); – user935988

0

이 시도 :

String input = "12"; 
System.out.println(input.charAt(0)); // gives '1' 
System.out.println(input.charAt(1)); // gives '2' 

또한, 당신이 '1'을하고자하는 경우 2 '를 문자열로 사용할 수 있습니다 (문자가 아님).

String firstDigit = input.charAt(0) + ""; 
String secondDigit = input.charAt(1) + ""; 

행운을 빕니다. !

콘스탄틴

편집은 '달'타입 java.util.Date의 변수가 있다고 가정하자. 다음 :

String monthToString = new SimpleDateFormat("MM").format(month); 
String firstDigit = monthToString.charAt(0) + ""; 
String secondDigit = monthToString.charAt(1) + ""; 
+0

안녕하세요.이 방법을 시도했습니다. – user935988

+0

System.out.println ("..."+ month.toString(). charAt (0)); \t \t System.out.println ("..."+ month.toString(). charAt (1)); \t \t 그것의주는 .. 그리고 .. – user935988

+0

그것은 '월'변수의 유형에 따라 다릅니다. 그것이 java.util.Date 인 경우, 다음을 사용해야합니다 : 새로운 SimpleDateFormat ("MM"). format (month) .charAt (0). 설명을 편집 했으므로 사용해보십시오. 이제 제대로 작동합니다. "= 새 SimpleDateFormat의 \t \t DateFormat의 달 ( –

0

사용 : 예를 들어

str.split("\\w.+") 

: 당신은 여기를보세요 수

String[] parts = "12".split("\\w.+"); 
String a = parts[0] 
Strign b = parts[1] 

http://www.roseindia.net/regularexpressions/splitting-string.shtml

+0

나는이 모든 방식으로 그 오류 또는 잘못된 형식을주는 날 도와 줘요 Date DATE_OF_REPORT = rs1.getDate ("DATE_OF_REPORT"); DateFormat month = new SimpleDateFormat ("MM"); – user935988

0

간단한 방법이 될 수 그것을 변환 에 문자열을 읽고 charAt()를 사용하여 하나씩 문자를 읽습니다.

는 숙제 질문 : 같은 소리

+0

나는 그것의주는 오류 또는 틀린 outputDate 모든 방법을 시도했다 DATE_OF_REPORT = rs1.getDate ("DATE_OF_REPORT"); DateFormat month = new SimpleDateFormat ("MM"); – user935988

+0

내가 잘못했거나 잘못된 출력을 내리려고 노력했다. Date DATE_OF_REPORT = rs1.getDate ("DATE_OF_REPORT"); DateFormat month = 새로운 SimpleDateFormat ("MM"); – user935988

0
String x = "12"; 
String[] x_arr= x.split(""); 
당신의 문자가 X에있는 것입니다

[1] X [2] 당신이 더 긴 문자열을 통과하면 결국 당신은 색인에 갈 수 있습니다 (1 년 같은). 빈 문자열이므로 x [0]은 사용하지 마십시오.

+0

나는이 방법을 시도했다. 여기 내 데이트 포맷이 필요하다. 나에게 도움이 될 수있다. Date DATE_OF_REPORT = rs1.getDate ("DATE_OF_REPORT"); DateFormat month = 새로운 SimpleDateFormat ("MM"); – user935988

+1

당신은 비호 환적입니다, 제 친구. 죄송합니다. –

0

String splits[] = "12".split("@?").

+0

날짜 DATE_OF_REPORT = rs1.getDate ("DATE_OF_REPORT"); DateFormat month = 새로운 SimpleDateFormat ("MM"); – user935988

+0

당신의 질문이 잘못 표현 된 것이 분명합니다. 당신은 그것을 심하게 바꾸고 싶을지도 모른다. – CamelSlack

관련 문제