Scala String Common Functions

The string in Scala is of String type, which is actually in Java java.lang.String. Its commonly used functions are as follows:

1. Substring-substring

substring()method returns a substring of a string.

substring()Methods have the following two forms:

  • substring(beginIndex: Int)beginIndex is the start index, and this form gets the substring from beginIndex to the end of the string.
  • substring(beginIndex: Int, endIndex: Int)beginIndex is the start index, and endIndex is the end index. This form gets the substring from beginIndex (including beginIndex) to endIndex (excluding endIndex).

The following diagram from the rookie tutorial is a good illustration:
substring - substringthe code example is as follows:

val str1 = "This is text"

println("返回值 :"+str1.substring(3))
println("返回值 :"+str1.substring(2, 7))
println("返回值 :"+str1.substring(2, str1.length))

operation result:

返回值 :s is text
返回值 :is is
返回值 :is is text

Note If endIndex exceeds the range of the string, String index out of range: 20an error will be reported.

2. String segmentation-split

split()The method splits the string based on matching the given regular expression, and the result is Array<String>.

Note : Escape characters such as ., $, and| must be added . Note : Multiple separators can be used as hyphens.*\\
|

split()Methods have the following two forms:

  • split(regex: String)regex is the regular expression delimiter.
  • split(regex: String, limit: Int)regex is the regular expression delimiter, and limit is the number of divisions, that is to say, only the first limit delimiters are divided.
    The sample code is as follows:
val str2 = "Welcome-to-here"

println("- 分隔符返回值 :")
str2.split("-").foreach(println)

System.out.println("- 分隔符设置分割份数返回值 :")
str2.split("-", 2).foreach(println)

val str3 = "java.lang.String"
System.out.println("转义字符返回值 :")
str3.split("\\.", 3).foreach(println)

val str4 = "acount=? and uu =? or n=?"
println("多个分隔符返回值 :")
str4.split("and|or").foreach(println)

operation result:

- 分隔符返回值 :
Welcome
to
here
- 分隔符设置分割份数返回值 :
Welcome
to-here
转义字符返回值 :
java
lang
String
多个分隔符返回值 :
acount=? 
 uu =? 
 n=?

3. Remove leading and trailing spaces -trim

trim()The method is used to delete the leading and trailing blank characters (including newline characters) of the string, and because it has no incoming parameters, the parentheses can be omitted. For example:

val str3 = "\n abc 12 \n "
println(str3.trim)
println("str3.trim length:"+str3.trim.length)
println(str3)
println("str3 length:"+str3.length)

operation result:

abc 12
str3.trim length:6

 abc 12 
 
str3 length:11

4. Conversion to and from numeric values

Depending on the type, the conversion of strings to numbers can be done in the following ways:

  • Int - an integer Integer.parseInt()
  • Long - long integer java.lang.Long.parseLong()
  • Float - floating point type java.lang.Float.parseFloat()
  • Double - Double-precision floating-point type java.lang.Double.parseDouble()

The sample code is as follows:

val str_int: String = "23"
val str_float: String = "233.3"

val my_int: Int = Integer.parseInt(str_int)
val my_long: Long = java.lang.Long.parseLong(str_int)
val my_float: Float = java.lang.Float.parseFloat(str_float)
val my_double: Double = java.lang.Double.parseDouble(str_float)

println(my_int)
println(my_long)
println(my_float)
println(my_double)

operation result:

23
23
233.3
233.3

Note that if the string format is not correct, java.lang.NumberFormatExceptionan exception will be thrown, which can be considered try/catchas a countermeasure:

var odd_value: Long = 0L
try {
    
    
  odd_value = java.lang.Long.parseLong(str_float)
} catch {
    
    
  case e: NumberFormatException => {
    
    
    e.printStackTrace()
    println("Wrong format")
  }
}

full code

StringTest.scala

package com.xxx

object StringTest {
    
    
  def main(args: Array[String]): Unit = {
    
    
    val str1 = "This is text"

    println("返回值 :"+str1.substring(3))
    println("返回值 :"+str1.substring(2, 7))
    println("返回值 :"+str1.substring(2, str1.length))

    val str2 = "Welcome-to-here"

    println("- 分隔符返回值 :")
    str2.split("-").foreach(println)

    System.out.println("- 分隔符设置分割份数返回值 :")
    str2.split("-", 2).foreach(println)

    val str3 = "java.lang.String"
    System.out.println("转义字符返回值 :")
    str3.split("\\.", 3).foreach(println)

    val str4 = "acount=? and uu =? or n=?"
    println("多个分隔符返回值 :")
    str4.split("and|or").foreach(println)

    val str5 = "\n abc 12 \n "
    println(str5.trim)
    println("str5.trim length:"+str5.trim.length)
    println(str5)
    println("str5 length:"+str5.length)

    val str_int: String = "23"
    val str_float: String = "233.3"

    val my_int: Int = Integer.parseInt(str_int)
    val my_long: Long = java.lang.Long.parseLong(str_int)
    val my_float: Float = java.lang.Float.parseFloat(str_float)
    val my_double: Double = java.lang.Double.parseDouble(str_float)

    println(my_int)
    println(my_long)
    println(my_float)
    println(my_double)

    var odd_value: Long = 0L
    try {
    
    
      odd_value = java.lang.Long.parseLong(str_float)
    } catch {
    
    
      case e: NumberFormatException => {
    
    
        e.printStackTrace()
        println("Wrong format")
      }
    }
  }
}

reference link

  1. Java substring() method | Novice Tutorial

Guess you like

Origin blog.csdn.net/willian113/article/details/130699052