scala 字符串函数_Scala字符串连接,子字符串,长度函数

Posted cunchi4221

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scala 字符串函数_Scala字符串连接,子字符串,长度函数相关的知识,希望对你有一定的参考价值。

scala 字符串函数

Scala String can be defined as a sequence of characters. Today we will look into String concatenation, substring and some other Scala string functions.

Scala字符串可以定义为字符序列。 今天,我们将研究String串联,子字符串和其他一些Scala字符串函数。

斯卡拉弦乐 (Scala String)

Consider an example of defining a string variable in Scala programming.

考虑一个在Scala编程中定义字符串变量的示例。

object Str 
     val st: String = "Scala is a functional programming language" 
def main(args:Array[String]) 
println(st)

Output: Scala is a functional programming language

输出 :Scala是一种功能编程语言

Strings in Scala are same as java string and hence the value is of type java.lang.String. Java classes are available in Scala, hence Scala makes use of java strings without creating a separate string class.

Scala中的字符串与java字符串相同,因此值的类型为java.lang.String 。 Scala中提供了Java类,因此Scala使用Java字符串而不创建单独的字符串类。

Similar to Java, String is immutable in Scala i.e. the object cannot be modified.

与Java类似, String在Scala中是不可变的 ,即无法修改对象。

在Scala中创建字符串 (Creating String in Scala)

Strings can be created by two ways as shown below.

可以通过以下两种方式创建字符串。

val s1 = "String is a sequence of characters"
val s2:String = "String is a sequence of characters"

In the first case the compilers encounters a string literal and creates a string object s1.

在第一种情况下,编译器遇到字符串文字并创建字符串对象s1。

In the second case, String type is specified before encountering the string literal.

在第二种情况下,在遇到字符串文字之前先指定String类型。

If we need to append to the original string, StringBuilder class is available in scala.

如果需要附加到原始字符串,可在scala中使用StringBuilder类。

Scala中的字符串长度 (String Length in Scala)

The methods used to obtain information about an object are known as accessor methods. The length() method is one of the accessor methods which returns the number of characters in the string.

用于获取有关对象的信息的方法称为访问器方法length()方法是访问器方法之一,该方法返回字符串中的字符数。

For example;

例如;

object strlen 
   def main(args: Array[String]) 
   var str = "Strings are immutable in scala";
   var len = str.length();
   println( "String Length is : " + len );
   

Output: String Length is : 30

输出 :字符串长度为:30

Scala字符串串联 (Scala String Concatenation)

Strings can be concatenated in two ways – one is using a concat method and the other is using the + operator.

字符串可以通过两种方式进行连接-一种使用concat方法,另一种使用+运算符。

Let us see an example of how to use concat method on strings.

让我们看一下如何在字符串上使用concat方法的示例。

object concat 
   def main(args: Array[String]) 
   var str1 = "String concatenation can be ";
 var str2 = "done using concat method";
   var st = str1.concat(str2)
   println( "Concatenated String is : " + st );
   

Output: Concatenated String is : String concatenation can be done using concat method

输出 :串联字符串是:字符串串联可以使用concat方法完成

The concat method accepts a string that is to be concatenated. Now let’s see an example of how to concatenate a string using + operator.

concat方法接受要串联的字符串。 现在,让我们看一个如何使用+运算符连接字符串的示例。

object strcon 
   def main(args: Array[String]) 
   var str1 = "Student name  ";
 var str2 = "is Micheal";
   var st = str1+ str2
   println( "Concatenated String is : " + st );
   

Output: Concatenated String is : Student name is Micheal

输出 :串联字符串是:学生名字是Micheal

Scala字符串格式 (Scala String Formatting)

The printf() and format() methods are used to format the output numbers. The String class has format() which returns a String object.

printf()format()方法用于格式化输出数字。 String类具有format()返回一个String对象。

object format 
   def main(args: Array[String]) 
      var n1 = 78.99
      var n2 = 49
      var s1 = "Hello, World!"
      var f1 = printf("The value of the float variable is " +
                   "%f, while the value of the integer " +
                   "variable is %d, and the string " +
                   "is %s", n1, n2, s1)
      println(f1)
   

Output: The value of the float variable is 78.990000, while the value of the integer variable is 49, and the string is Hello, World!()

输出 :float变量的值为78.990000,而整数变量的值为49,字符串为Hello,World!()

The format method can be used as below.

格式化方法可以如下使用。

object formatmethod 
  def main(args:Array[String])
 val firstName = "Chris"
 val lastName = "Harris"
 val age = 12;
 
  println("%s %s, %d".format(firstName, lastName, age));
  

Output: Chris Harris, 12

输出 :克里斯·哈里斯(Chris Harris),12岁

Scala字符串函数 (Scala String Functions)

Some of the string useful methods in Scala are;

Scala中一些有用的字符串方法是:

  1. char charAt(int index) → Returns the character at the specified index.

    char charAt(int index) →返回指定索引处的字符。
  2. String replace(char c1, char c2) → Returns a new string resulting by replacing all occurrences of c1 in this string with c2.

    字符串replace(char c1,char c2) →返回一个新字符串,该字符串是用c2替换此字符串中所有出现的c1的结果。
  3. String[] split(String reg1) → Splits this string around matches of the given regular expression.

    String [] split(String reg1) →将该字符串拆分为给定正则表达式的匹配项。
  4. String substring(int i1) → Returns a new string that is a substring of this string.

    字符串substring(int i1) →返回一个新字符串,该字符串是该字符串的子字符串。
  5. String trim() → Returns a copy of the string, with leading and trailing whitespace omitted.

    字符串trim() →返回字符串的副本,省略前导和尾随空格。
  6. String substring(int b1, int e1) → Returns a new string that is a substring of this string.

    字符串substring(int b1,int e1) →返回一个新字符串,该字符串是该字符串的子字符串。
  7. boolean startsWith(String prefix) → Tests if this string starts with the specified prefix.

    boolean startsWith(String prefix) →测试此字符串是否以指定的前缀开头。
  8. boolean matches(String regex) → Tells whether or not this string matches the given regular expression.

    boolean match(String regex) →判断此字符串是否与给定的正则表达式匹配。
  9. int hashCode() → Returns a hash code for this string.

    int hashCode() →返回此字符串的哈希码。

That’s all for a quick roundup of String in Scala, we will look into more scala core features in coming posts.

这就是在Scala中快速汇总String的全部内容,我们将在以后的文章中探讨更多scala核心功能。

翻译自: https://www.journaldev.com/8407/scala-string-concatenation-substring-length-functions

scala 字符串函数

以上是关于scala 字符串函数_Scala字符串连接,子字符串,长度函数的主要内容,如果未能解决你的问题,请参考以下文章

Scala 中的高效字符串连接

scala学习遇到的坑

Json.obj Scala,字符串连接:编译错误

Scala:如何进行字符串连接以避免 GC 开销问题

Scala 基础:Scala变量 标识符

Scala 中下划线 _ 的用法总结