Groovyで文字列を整数に変換する
1. 概要
この短いチュートリアルでは、GroovyでStringからIntegerに変換するさまざまな方法を紹介します。
2. asでキャスト
変換に使用できる最初のメソッドは、 as キーワードです。これは、クラスのasType()メソッドを呼び出すのと同じです。
@Test
void givenString_whenUsingAsInteger_thenConvertToInteger() {
def stringNum = "123"
Integer expectedInteger = 123
Integer integerNum = stringNum as Integer
assertEquals(integerNum, expectedInteger)
}
上記のように、をintとして使用できます。
@Test
void givenString_whenUsingAsInt_thenConvertToInt() {
def stringNum = "123"
int expectedInt = 123
int intNum = stringNum as int
assertEquals(intNum, expectedInt)
}
3. toInteger
もう1つの方法は、java.lang.CharSequenceのGroovyJDK拡張機能からのものです。
@Test
void givenString_whenUsingToInteger_thenConvertToInteger() {
def stringNum = "123"
int expectedInt = 123
int intNum = stringNum.toInteger()
assertEquals(intNum, expectedInt)
}
4. Integer#parseInt
3番目の方法は、 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
別の方法は、新しい Integer オブジェクトを作成し、そのintValueメソッドを呼び出すことです。
@Test
void givenString_whenUsingIntValue_thenConvertToInteger() {
def stringNum = "123"
int expectedInt = 123
int intNum = new Integer(stringNum).intValue()
assertEquals(intNum, expectedInt)
}
または、この場合、 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
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
最後のメソッドでは、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. 例外処理
したがって、は、数値以外の文字がある場合など、変換が失敗した場合、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()
}
これを防ぐために、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. 概要
この短い記事では、GroovyでStringからIntegerオブジェクトに切り替えるための効果的な方法をいくつか紹介しました。
オブジェクトタイプを変換するための最良の方法を選択する場合、上記のすべてが同等に優れています。最も重要なことは、最初に文字列の値がアプリケーションは、非数値、空、またはnullにすることができます。
いつものように、すべてのコード例はGitHubのにあります。