1. Overview
1.概述
In this short tutorial, we’ll show different ways to convert from String to Integer in Groovy.
在这个简短的教程中,我们将展示在Groovy中从String转换为Integer的不同方法。
2. Casting with as
2. 用as进行铸造
The first method that we can use for the conversion is the as keyword, which is the same as calling the class’s asType() method:
我们可以使用的第一个转换方法是as关键字,这与调用类的asType()方法相同。
@Test
void givenString_whenUsingAsInteger_thenConvertToInteger() {
def stringNum = "123"
Integer expectedInteger = 123
Integer integerNum = stringNum as Integer
assertEquals(integerNum, expectedInteger)
}
Like the above, we can use as int:
像上面一样,我们可以使用as int。
@Test
void givenString_whenUsingAsInt_thenConvertToInt() {
def stringNum = "123"
int expectedInt = 123
int intNum = stringNum as int
assertEquals(intNum, expectedInt)
}
3. toInteger
3、toInteger
Another method is from the Groovy JDK extension for java.lang.CharSequence:
另一个方法是来自Groovy JDK扩展的java.lang.CharSequence。
@Test
void givenString_whenUsingToInteger_thenConvertToInteger() {
def stringNum = "123"
int expectedInt = 123
int intNum = stringNum.toInteger()
assertEquals(intNum, expectedInt)
}
4. Integer#parseInt
4.Integer#parseInt
A third way is to use Java’s static method Integer.parseInt():
第三种方法是使用Java的静态方法Integer.parseInt()。
@Test
void givenString_whenUsingParseInt_thenConvertToInteger() {
def stringNum = "123"
int expectedInt = 123
int intNum = Integer.parseInt(stringNum)
assertEquals(intNum, expectedInt)
}
5. Integer#intValue
5.Integer#intValue
An alternative method is to create a new Integer object and call its intValue method:
另一种方法是创建一个新的Integer对象并调用其intValue方法。
@Test
void givenString_whenUsingIntValue_thenConvertToInteger() {
def stringNum = "123"
int expectedInt = 123
int intNum = new Integer(stringNum).intValue()
assertEquals(intNum, expectedInt)
}
Or, in this case, we can also use just new Integer(stringNum):
或者,在这种情况下,我们也可以只使用new Integer(stringNum)。
@Test
void givenString_whenUsingNewInteger_thenConvertToInteger() {
def stringNum = "123"
int expectedInt = 123
int intNum = new Integer(stringNum)
assertEquals(intNum, expectedInt)
}
6. Integer#valueOf
6.Integer#valueOf
Similar to Integer.parseInt(), we can also use Java’s static method Integer#valueOf:
与Integer.parseInt()类似,我们也可以使用Java的静态方法Integer#valueOf。
@Test
void givenString_whenUsingValueOf_thenConvertToInteger() {
def stringNum = "123"
int expectedInt = 123
int intNum = Integer.valueOf(stringNum)
assertEquals(intNum, expectedInt)
}
7. DecimalFormat
7. DecimalFormat.
And for our last method, we can apply Java’s DecimalFormat class:
而对于我们的最后一个方法,我们可以应用Java的DecimalFormat类。
@Test
void givenString_whenUsingDecimalFormat_thenConvertToInteger() {
def stringNum = "123"
int expectedInt = 123
DecimalFormat decimalFormat = new DecimalFormat("#")
int intNum = decimalFormat.parse(stringNum).intValue()
assertEquals(intNum, expectedInt)
}
8. Exception Handling
8.异常处理
So, if conversion fails, like if there are non-numeric characters, a NumberFormatException will be thrown. Additionally, in the case where String is null, NullPointerException will be thrown:
所以,如果转换失败,比如有非数字字符,将抛出NumberFormatException。此外,如果String是null,NullPointerException将被抛出。
@Test(expected = NumberFormatException.class)
void givenInvalidString_whenUsingAs_thenThrowNumberFormatException() {
def invalidString = "123a"
invalidString as Integer
}
@Test(expected = NullPointerException.class)
void givenNullString_whenUsingToInteger_thenThrowNullPointerException() {
def invalidString = null
invalidString.toInteger()
}
To prevent this from happening, we can use the isInteger method:
为了防止这种情况发生,我们可以使用isInteger方法。
@Test
void givenString_whenUsingIsInteger_thenCheckIfCorrectValue() {
def invalidString = "123a"
def validString = "123"
def invalidNum = invalidString?.isInteger() ? invalidString as Integer : false
def correctNum = validString?.isInteger() ? validString as Integer : false
assertEquals(false, invalidNum)
assertEquals(123, correctNum)
}
9. Summary
9.摘要
In this short article, we’ve shown some effective ways to switch from String to Integer objects in Groovy.
在这篇短文中,我们展示了一些在Groovy中从String 切换到Integer对象的有效方法。
When it comes to choosing the best method to convert an object type, all of the above are equally good. The most important thing is to avoid errors by first checking if the value of the String in our application can be non-numeric, empty, or null.
当谈到选择转换对象类型的最佳方法时,上述所有方法都是同样好的。最重要的是避免错误,首先检查我们应用程序中String的值是否可以是非数字、空或null。
As usual, all code examples can be found over on GitHub.
像往常一样,所有的代码例子都可以在GitHub上找到。