日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Scala程序将字符串转换为整数

發(fā)布時(shí)間:2025/3/11 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Scala程序将字符串转换为整数 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

In Scala, there is a huge library to support different operations on a string. One such operation is to convert string to int in Scala.

在Scala中,有一個(gè)龐大的庫來支持對字符串的不同操作。 一種這樣的操作是在Scala中將字符串轉(zhuǎn)換為int。

A string can be converted to integer in Scala using the toInt method.

可以使用toInt方法在Scala 中將字符串轉(zhuǎn)換為整數(shù)。

Syntax:

句法:

string.toInt

This will return the integer conversion of the string. If the string does not contain an integer it will throw an exception with will be NumberFormatException.

這將返回字符串整數(shù)轉(zhuǎn)換 。 如果字符串不包含整數(shù),它將引發(fā)一個(gè)異常,該異常將為NumberFormatException

So, the statement: val i = "Hello".toInt will throw an exception instead of an integer.

因此,語句: val i =“ Hello” .toInt將引發(fā)異常而不是整數(shù)。

So, we need to handle this exception and we will do this using the try-catch block and print "The string in not integer" when the string to be converted does not contain an integer value.

因此,我們需要處理此異常,我們將使用try-catch塊來執(zhí)行此操作,并要轉(zhuǎn)換的字符串不包含整數(shù)值時(shí)顯示“非整數(shù)的字符串

在Scala中將字符串轉(zhuǎn)換為整數(shù)的程序 (Program to convert string to integer in Scala)

object MyClass {def main(args: Array[String]) {val string = "1C 2C++ 3Java"println(string)val stringContents = string.split("\\d+")println("Content of the string are: ")for(i <- 0 to stringContents.length-1)println(stringContents(i))} }

Output

輸出量

The string is : 12345 Integer Conversion of string is 12345

翻譯自: https://www.includehelp.com/scala/convert-string-to-integer.aspx

總結(jié)

以上是生活随笔為你收集整理的Scala程序将字符串转换为整数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。