Java Tutorial/Data Type/Convert from String
Содержание
- 1 Convert a String to Date
- 2 Convert base64 string to a byte array
- 3 Converting a String to a byte Number
- 4 Converting a String to a int(integer) Number
- 5 Converting a String to a short Number
- 6 Convert string of time to time object
- 7 Convert String to character array
- 8 Integer.parseInt(): Converting String to int
- 9 Integer.valueOf: Converting String to Integer
- 10 Number Parsing
- 11 Parse basic types
- 12 String.ValueOf
- 13 sums a list of numbers entered by the user
Convert a String to Date
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date theDate = dateFormat.parse("01/01/2009");
System.out.println(dateFormat.format(theDate));
}
}
//01/01/2009
Convert base64 string to a byte array
public class Main {
public static void main(String[] argv) throws Exception {
byte[] buf = new byte[] { 0x12, 0x23 };
String s = new sun.misc.BASE64Encoder().encode(buf);
buf = new sun.misc.BASE64Decoder().decodeBuffer(s);
}
}
Converting a String to a byte Number
public class Main {
public static void main(String[] argv) throws Exception {
byte b = Byte.parseByte("123");
System.out.println(b);
}
}
Converting a String to a int(integer) Number
public class Main {
public static void main(String[] argv) throws Exception {
int i = Integer.parseInt("123");
System.out.println(i);
}
}
Converting a String to a short Number
public class Main {
public static void main(String[] argv) throws Exception {
short s = Short.parseShort("123");
System.out.println(s);
}
}
Convert string of time to time object
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception {
String time = "15:30:18";
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date = sdf.parse(time);
System.out.println("Date and Time: " + date);
}
}
Convert String to character array
public class Main {
public static void main(String[] args) {
String str = "Abcdefg";
char[] cArray = str.toCharArray();
for (char c : cArray)
System.out.println(c);
}
}
/*
A
b
c
d
e
f
g
*/
Integer.parseInt(): Converting String to int
public class MainClass {
public static void main(String[] arg) {
System.out.println(Integer.parseInt("100"));
}
}
100
Integer.valueOf: Converting String to Integer
public class MainClass {
public static void main(String[] arg) {
System.out.println(Integer.valueOf("00900"));
}
}
900
Number Parsing
Parsing is to do with the conversion of a string into a number or a date.
The purpose of number parsing is to convert a string into a numeric primitive type. Byte, Short, Integer, Long, Float, and Double classes, provide static methods to parse strings. For example, the Integer class has the parseInteger method with the following signature.
public static int parseInt (String s) throws NumberFormatException
Parse basic types
/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* Parse basic types.
*
* @author Christoph Beck
*/
public class ParseUtils {
private static Locale locale = Locale.US;
private static Object nullValue(Class type) {
if (type.isPrimitive()) {
if (type == boolean.class)
return Boolean.FALSE;
if (type == byte.class)
return new Byte((byte)0);
if (type == char.class)
return new Character((char)0);
if (type == short.class)
return new Short((short)0);
if (type == int.class)
return new Integer(0);
if (type == long.class)
return new Long(0);
if (type == float.class)
return new Float(0);
if (type == double.class)
return new Double(0);
}
return null;
}
private static Class objectType(Class type) {
if (type.isPrimitive()) {
if (type == boolean.class)
return Boolean.class;
if (type == byte.class)
return Byte.class;
if (type == char.class)
return Character.class;
if (type == short.class)
return Short.class;
if (type == int.class)
return Integer.class;
if (type == long.class)
return Long.class;
if (type == float.class)
return Float.class;
if (type == double.class)
return Double.class;
}
return type;
}
private static Object parse(Format format, String value) throws ParseException {
ParsePosition pos = new ParsePosition(0);
Object result = format.parseObject(value, pos);
if (pos.getIndex() < value.length())
throw new ParseException("Cannot parse " + value + " (garbage suffix)!", pos.getIndex());
return result;
}
private static Date parseDate(String value) throws ParseException {
DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, locale);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
return (Date)parse(format, value);
}
private static Boolean parseBoolean(String value) throws ParseException {
if ("true".equals(value)) {
return Boolean.TRUE;
} else if ("false".equals(value)) {
return Boolean.FALSE;
} else {
throw new ParseException("Cannot parse "" + value + "" as boolean", 0);
}
}
private static Character parseCharacter(String value) throws ParseException {
if (value.length() != 1) {
throw new ParseException("Cannot parse "" + value + "" as character", value.length());
}
return new Character(value.charAt(0));
}
/**
* Parse value of specified type. The string value has to be in
* standard notation for the specified type.
*/
public static Object parse(Class type, String value) throws Exception {
if (value == null) {
return nullValue(type);
} else if (value.length() == 0) {
return type == String.class ? value : nullValue(type);
}
type = objectType(type);
if (type == BigDecimal.class) {
return new BigDecimal(value);
} else if (type == BigInteger.class) {
return new BigInteger(value);
} else if (type == Boolean.class) {
return parseBoolean(value);
} else if (type == Byte.class) {
return Byte.valueOf(value);
} else if (type == Character.class) {
return parseCharacter(value);
} else if (type == Date.class) {
return parseDate(value);
} else if (type == Double.class) {
return Double.valueOf(value);
} else if (type == Float.class) {
return Float.valueOf(value);
} else if (type == Integer.class) {
return Integer.valueOf(value);
} else if (type == Long.class) {
return Long.valueOf(value);
} else if (type == Short.class) {
return Short.valueOf(value);
} else if (type == String.class) {
return value;
}
throw new ParseException("Cannot parse type " + type, 0);
}
}
///////////////////////
/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.base.util;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import junit.framework.TestCase;
/**
* ParseUtils test case.
*
* @author Christoph Beck
*/
public class ParseUtilsTest extends TestCase {
/**
* Constructor for ParseUtilsTest.
* @param arg0
*/
public ParseUtilsTest(String arg0) {
super(arg0);
}
public void testNullPrimitive() throws Exception {
assertEquals(Boolean.FALSE, ParseUtils.parse(boolean.class, null));
assertEquals(new Character((char)0), ParseUtils.parse(char.class, null));
assertEquals(new Byte((byte)0), ParseUtils.parse(byte.class, null));
assertEquals(new Short((short)0), ParseUtils.parse(short.class, null));
assertEquals(new Integer(0), ParseUtils.parse(int.class, null));
assertEquals(new Long(0), ParseUtils.parse(long.class, null));
assertEquals(new Float(0), ParseUtils.parse(float.class, null));
assertEquals(new Double(0), ParseUtils.parse(double.class, null));
}
public void testPrimitive() throws Exception {
assertEquals(Boolean.TRUE, ParseUtils.parse(boolean.class, "true"));
assertEquals(Boolean.FALSE, ParseUtils.parse(boolean.class, "false"));
assertEquals(new Character((char)10), ParseUtils.parse(char.class, "\n"));
assertEquals(new Byte((byte)10), ParseUtils.parse(byte.class, "10"));
assertEquals(new Short((short)10), ParseUtils.parse(short.class, "10"));
assertEquals(new Integer(10), ParseUtils.parse(int.class, "10"));
assertEquals(new Long(10), ParseUtils.parse(long.class, "10"));
assertEquals(new Float(10), ParseUtils.parse(float.class, "10"));
assertEquals(new Double(10), ParseUtils.parse(double.class, "10"));
}
public void testNullObject() throws Exception {
assertNull(ParseUtils.parse(Boolean.class, null));
assertNull(ParseUtils.parse(Byte.class, null));
assertNull(ParseUtils.parse(Character.class, null));
assertNull(ParseUtils.parse(Short.class, null));
assertNull(ParseUtils.parse(Integer.class, null));
assertNull(ParseUtils.parse(Long.class, null));
assertNull(ParseUtils.parse(Float.class, null));
assertNull(ParseUtils.parse(Double.class, null));
assertNull(ParseUtils.parse(BigInteger.class, null));
assertNull(ParseUtils.parse(BigDecimal.class, null));
assertNull(ParseUtils.parse(Date.class, null));
assertNull(ParseUtils.parse(String.class, null));
}
public void testObject() throws Exception {
assertEquals(Boolean.TRUE, ParseUtils.parse(Boolean.class, "true"));
assertEquals(Boolean.FALSE, ParseUtils.parse(Boolean.class, "false"));
assertEquals(new Character((char)10), ParseUtils.parse(Character.class, "\n"));
assertEquals(new Byte((byte)10), ParseUtils.parse(Byte.class, "10"));
assertEquals(new Short((short)10), ParseUtils.parse(Short.class, "10"));
assertEquals(new Integer(10), ParseUtils.parse(Integer.class, "10"));
assertEquals(new Long(10), ParseUtils.parse(Long.class, "10"));
assertEquals(new Float(10), ParseUtils.parse(Float.class, "10"));
assertEquals(new Double(10), ParseUtils.parse(Double.class, "10"));
assertEquals(new BigInteger("10"), ParseUtils.parse(BigInteger.class, "10"));
assertEquals(new BigDecimal(10), ParseUtils.parse(BigDecimal.class, "10"));
assertEquals(new Date(0), ParseUtils.parse(Date.class, "1/1/70"));
assertEquals("foo", ParseUtils.parse(String.class, "foo"));
}
public void testBadValues() throws Exception {
try {
ParseUtils.parse(Boolean.class, "no");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Character.class, "10");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Byte.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Short.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Integer.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Long.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Float.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Double.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(BigInteger.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(BigDecimal.class, "abc");
fail("Exception expected");
} catch(Exception e) {}
try {
ParseUtils.parse(Date.class, "1.1.70");
fail("Exception expected");
} catch(Exception e) {}
}
public void testBadType() throws Exception {
try {
ParseUtils.parse(Cloneable.class, "dolly");
fail("Exception expected");
} catch(Exception e) {}
}
public static void main(String[] args) {
junit.textui.TestRunner.run(ParseUtilsTest.class);
}
}
String.ValueOf
public class MainClass
{
public static void main( String args[] )
{
char charArray[] = { "a", "b", "c", "d", "e", "f" };
boolean booleanValue = true;
char characterValue = "Z";
int integerValue = 7;
long longValue = 10000000000L; // L suffix indicates long
float floatValue = 2.5f; // f indicates that 2.5 is a float
double doubleValue = 33.333; // no suffix, double is default
Object objectRef = "hello"; // assign string to an Object reference
System.out.printf("char array = %s\n", String.valueOf( charArray ) );
System.out.printf("part of char array = %s\n",String.valueOf( charArray, 3, 3 ) );
System.out.printf("boolean = %s\n", String.valueOf( booleanValue ) );
System.out.printf("char = %s\n", String.valueOf( characterValue ) );
System.out.printf("int = %s\n", String.valueOf( integerValue ) );
System.out.printf("long = %s\n", String.valueOf( longValue ) );
System.out.printf("float = %s\n", String.valueOf( floatValue ) );
System.out.printf("double = %s\n", String.valueOf( doubleValue ) );
System.out.printf("Object = %s\n", String.valueOf( objectRef ) );
} // end main
}
char array = abcdef part of char array = def boolean = true char = Z int = 7 long = 10000000000 float = 2.5 double = 33.333 Object = hello
sums a list of numbers entered by the user
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class ParseDemo {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
int i;
int sum = 0;
System.out.println("Enter numbers, 0 to quit.");
do {
str = br.readLine();
try {
i = Integer.parseInt(str);
} catch (NumberFormatException e) {
System.out.println("Invalid format");
i = 0;
}
sum += i;
System.out.println("Current sum is: " + sum);
} while (i != 0);
}
}