博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scala 字符串函数_Scala字符串连接,子字符串,长度函数
阅读量:2530 次
发布时间:2019-05-11

本文共 5624 字,大约阅读时间需要 18 分钟。

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, in Scala i.e. the object cannot be modified.

与Java类似, 在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核心功能。

翻译自:

scala 字符串函数

转载地址:http://guqzd.baihongyu.com/

你可能感兴趣的文章
Android dex分包方案
查看>>
ThreadLocal为什么要用WeakReference
查看>>
删除本地文件
查看>>
FOC实现概述
查看>>
base64编码的图片字节流存入html页面中的显示
查看>>
这个大学时代的博客不在维护了,请移步到我的新博客
查看>>
GUI学习之二十一——QSlider、QScroll、QDial学习总结
查看>>
nginx反向代理docker registry报”blob upload unknown"解决办法
查看>>
gethostbyname与sockaddr_in的完美组合
查看>>
kibana的query string syntax 笔记
查看>>
旋转变换(一)旋转矩阵
查看>>
thinkphp3.2.3 bug集锦
查看>>
[BZOJ 4010] 菜肴制作
查看>>
C# 创建 读取 更新 XML文件
查看>>
KD树
查看>>
VsVim - Shortcut Key (快捷键)
查看>>
C++练习 | 模板与泛式编程练习(1)
查看>>
HDU5447 Good Numbers
查看>>
08.CXF发布WebService(Java项目)
查看>>
java-集合框架
查看>>