scala字符串的拉链操作

Scala字符串操作 (Scala strings operation)

A string is a very important datatype in Scala. This is why there are a lot of operations that can be done on the string object. Since the regular operations like addition, subtraction is not valid for a string, therefore, special operations like concatenation, comparison are defined.

字符串是Scala中非常重要的数据类型。 这就是为什么可以对字符串对象执行许多操作的原因。 由于常规操作(如加法,减法)对于字符串无效,因此,定义了特殊操作(如串联,比较)。

In this tutorial, we will share some of the important and common string functions.

在本教程中,我们将分享一些重要和通用的字符串函数

1)字符串相等(==) (1) String Equality (==))

The tradition equality operator == is also available in the string. You can use it to find equality of two string and the equality results in a boolean value.

字符串中也可以使用传统的等于运算符== 。 您可以使用它来查找两个字符串的相等性,并且相等性会产生布尔值。

    str1 = str2 // this will return either TRUE OR FALSE

For Example,

例如,

    val str1 = "Include"
val str2 = "Includes"
str1 == str2 // this will be false

2)字符串长度 (2) String Length)

There is an inbuilt function that is used to find the number of characters in a string. It includes all the space that comes it between. It is used to set the limit for string traversal.

有一个内置函数,用于查找字符串中的字符数。 它包括介于两者之间的所有空间。 用于设置字符串遍历的限制。

    // this outputs a positive integer denoting the // number of characters in the array. str1.length

For example,

例如,

    val str1 = "Include help"
str1.length // this will print 12

3)串连音 (3) String concat)

You can concatenate a string on other. It means the string will be appended on the calling string.

您可以在其他字符串上串联一个字符串。 这意味着该字符串将附加在调用字符串上。

    str1.concat(str2)

for example,

例如,

    val str1 = "Include"
val str2 = "Help"
str1.concat(str2) // This will print IncludeHelp

4)字符串charAt()方法 (4) String charAt() method )

To print the character at a specific index of the string the charAt method is used. The input is a zero-based index and output will be the corresponding character. Will throw an error if the input is greater than the length of the string.

要在字符串的特定索引处打印字符,请使用charAt方法。 输入是从零开始的索引,输出将是相应的字符。 如果输入大于字符串的长度,将引发错误。

    str1.charAt(n)

For example,

例如,

    val name = "Include"
name.charAt(4) // This will output u.

5)indexOf()方法 (5) indexOf() method)

The indexOf() method is used to check the index of a character in a string. This method returns an integer which is positive within the length of the string when the character is found in the array otherwise -1 is given as output.

indexOf()方法用于检查字符串中字符的索引。 当在数组中找到字符时,此方法返回一个整数,该整数在字符串的长度内为正,否则将给出-1作为输出。

    str1.indexOf("a")

For example,

例如,

    val name = "Include"
name.indexOf("d") // This will output 5.

6)Substring()方法 (6) Substring() method)

The substring method is used to define a substring from the calling string. It makes a new string with the specified part of the string.

substring方法用于从调用字符串中定义一个子字符串。 它使用字符串的指定部分创建一个新字符串。

    str2 =  str1.substring(startIndex , endIndex)

For example,

例如,

    val name = "IncludeHelp is awesome"
name.substring(14 , 21) // This will output awesome.

Example code that uses all these functions

使用所有这些功能的示例代码

object MyClass {def main(args: Array[String]) {val str1 = "include Help "
val str2 = "is awesome"
println("str1 = " + str1)
println("str2 = " + str2)
println("Comparison between str1 and str2 is " + (str1 == str2))
println("The length of str1 is " + str1.length)
println("Concatenating str1 with str2 gives \n " + str1.concat(str2))
println("The character at  index 5 of str2 is" + str2.charAt(5))
println("The index of 'c' in str1 is " + str1.indexOf("c"))
}
}

Output

输出量

str1 = include Help
str2 = is awesome
Comparison between str1 and str2 is false
The length of str1 is 13
Concatenating str1 with str2 gives include Help is awesome
The character at  index 5 of str2 ise
The index of 'c' in str1 is 2

翻译自: https://www.includehelp.com/scala/operation-on-strings-in-scala.aspx

scala字符串的拉链操作

scala字符串的拉链操作_在Scala中对字符串进行操作相关推荐

  1. java如果把字符串转成对象_为什么Java中的字符串对象是不可变的,有什么好处?...

    专注于Java领域优质技术号,欢迎关注 原创: 阿杜的世界 阅读本文大概需要 4分钟. 所谓不可变对象,是指一个对象在创建后,它的内部状态不会被改变的对象.这意味着当我们将一个不可变对象的引用赋值给某 ...

  2. python输出字符串后三位_在python中,字符串s =

    [单选题][图片] [单选题]This kind of car _____ made in Shanghai. [判断题]真正衡量数字通信系统的有效性指标是频带利用率. [单选题]小王正在考虑在他的总 ...

  3. mysql查找内容某字符串出现的次数_查找MySQL中特定字符串出现的次数?

    使用LENGTH()此.让我们首先创建一个表-mysql> create table DemoTable -> ( -> Value text -> ); 使用插入命令在表中插 ...

  4. mysql遍历字符串字符的函数_在MySQL中遍历字符串

    一.当字符串之间没有分隔符 遍历字符串时我们需要一个辅助表与原表做连接查询 辅助表中的数字就是表示字符串中各个字符的位置 要求该辅助表必须有足够多的行数保证循环操作的次数 例:把emp表中等于king ...

  5. 写一个函数,求一个字符串的长度,在main 函数中输入字符串,并输出其长度。

    // 写一个函数,求一个字符串的长度,在main 函数中输入字符串,并输出其长度. #include <stdio.h> main() {  int len;  char*str[20]; ...

  6. 习题 8.6 写一函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度。

    C程序设计(第四版) 谭浩强 习题8.6 个人设计 习题 8.6 写一函数,求一个字符串的长度.在main函数中输入字符串,并输出其长度. 代码块: 方法1: #include <stdio.h ...

  7. 写一个函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度

    写一个函数,求一个字符串的长度.在main函数中输入字符串,并输出其长度 代码如下: #include<stdio.h> int len(char *p); int main() {int ...

  8. 题8.6:写一函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度。

    题目 本题是谭浩强<C程序设计课后习题>题8.6. 题目: 写一函数,求一个字符串的长度.在main函数中输入字符串,并输出其长度. 以下是本篇文章正文内容,欢迎朋友们进行指正,一起探讨, ...

  9. 写一函数,求一个字符串的长度。在main函数中输入字符串,并输出其长度。

    写一函数,求一个字符串的长度.在main函数中输入字符串,并输出其长度. 解题思路: 字符串以\0作为结尾,则从第一个字符开始向后移动遇到\0认为字符串结束. 答案: #include <std ...

最新文章

  1. 【数据架构解读】基于阿里云数加StreamCompute和MaxCompute构建的访问日志统计分析...
  2. python创建新文件-如何在python中编辑文件并创建一个新的文件?
  3. 好记性不如烂笔头:会议纪要本
  4. java加密方式有哪些_面完平安JAVA,他们说了这些
  5. Ubuntu 12.04下安装Oracle Express 11gR2
  6. 分布式服务框架XXL-RPC
  7. python爬虫应用实例_Python爬虫进阶必备 | 一个典型的 AES 加密在爬虫中的应用案例...
  8. python分布式框架celery项目开发_本项目在 Celery 分布式爬虫的基础上构建监控方案 Demo...
  9. 9.Springcloud的Hystrix服务熔断和服务降级
  10. 减小Gcc编译程序的体积
  11. Awesome Go
  12. CGAffineTransformMakeRotation 实现旋转
  13. UVALive - 8295 Triangle to Hexagon
  14. Android IntentService的使用和源代码分析
  15. GBin1分享:一个漂亮的jQuery页面内容导航插件 - Flexiable Nav
  16. 移动端设备广告投放的用户唯一识别码
  17. linux调整tmp目录,linux – 如何将默认/ tmp更改为/ home / user / tmp
  18. 7款英文语法检查工具推荐
  19. 软件领域专利申请的特点及案例
  20. 数据库的连接 SQL Joins

热门文章

  1. 日志长度_Kafka 日志存储详解
  2. jert oracle 统计说明,Oracle JET简单入门(一)Oracle JET介绍
  3. Tips_一级菜单栏实现
  4. require.context
  5. 仿淘宝网站的TabPage导航效果
  6. java面向对象中的抽象,类与对象
  7. Cmder - 在右键菜单添加Cmder Here
  8. go语音实战读后感——一
  9. JDBC常用API小结
  10. 模型和控制器-起步阶段