scala 拆分字符串翻转_Scala程序分割字符串

scala 拆分字符串翻转

A string is a collection that stores multiple characters, it is an immutable sequence which cannot be changed.

字符串是存储多个字符的集合,它是不可更改的不可更改的序列。

分割字符串 (Splitting a string)

In Scala, using the split() method one can split the string in an array based on the required separator like commas, spaces, line-breaks, symbols or any regular expression.

在Scala中,使用split()方法可以根据所需的分隔符(例如逗号,空格,换行符,符号或任何正则表达式)将字符串拆分为数组。

Syntax:

句法:

    string.split("saperator")

程序在Scala中分割字符串 (Program to split a string in Scala)

object MyClass {
def main(args: Array[String]) {
val string = "Hello! Welcome to includeHelp..."
println(string)
val stringContents = string.split(" ")
println("Content of the string are: ")
for(i <- 0 to stringContents.length-1)
println(stringContents(i))
}
}

Output

输出量

Hello! Welcome to includeHelp...
Content of the string are: 
Hello!
Welcome
to
includeHelp...

This method is useful when we are working with data that are encoded in a string like URL-encoded string, multiline data from servers, etc where the string need to be split to extract information. One major type is when data comes in the form of CSV (comma-separated value) strings which is most common and needs each value to be separated from the string.

当我们使用以字符串编码的数据(例如URL编码的字符串,来自服务器的多行数据)等需要对字符串进行拆分以提取信息时,此方法很有用。 一种主要类型是当数据以CSV(逗号分隔值)字符串的形式出现时,这是最常见的字符串,需要将每个值与字符串分开。

Let's see how to,

让我们看看如何

程序使用拆分方法拆分CSV字符串 (Program to split a CSV string using split method)

object MyClass {
def main(args: Array[String]) {
val CSVstring = "ThunderBird350 , S1000RR, Iron883, StreetTripleRS"
println(CSVstring)
val stringContents = CSVstring.split(", ")
println("Content of the string are: ")
for(i <- 0 to stringContents.length-1)
println(stringContents(i))
}
}

Output

输出量

ThunderBird350 , S1000RR, Iron883, StreetTripleRS
Content of the string are: 
ThunderBird350 
S1000RR
Iron883
StreetTripleRS

使用正则表达式分割字符串 (Splitting String using Regular Expressions)

In the split method, a regular expression(regex) can also be used as a separator to split strings.

在split方法中,正则表达式(regex)也可用作分隔符以分隔字符串。

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

输出量

1C 2C++ 3Java
Content of the string are: C 
C++ 
Java

Here, we have separated the string using the regex \\d+ which considers any digit. In our string, the function will split every time a digit is encountered.

在这里,我们使用考虑任何数字的正则表达式\\ d +分隔了字符串。 在我们的字符串中,该函数将在每次遇到数字时拆分。

翻译自: https://www.includehelp.com/scala/split-string.aspx

scala 拆分字符串翻转

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/540633.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[转载] python 简单示例说明os.walk和os.path.walk的不同

参考链接&#xff1a; 示例说明Python2.x和Python3.x之间的重要区别 import os,os.path def func(arg,dirname,names): for filespath in names: print os.path.join(dirname,filespath) if __name__"__main__": print "os.walk" index 1 for root,subd…

c#中索引器是什么_C#中的索引器

c#中索引器是什么An Indexer is a special feature of C# to use an object as an array. If you define an indexer in a class then it will behave like a virtual array. Indexer is also known as smart array in C#. It is not a compulsory or essential part of OOPS. …

asp.net MVC5为WebAPI添加命名空间的支持

前言 默认情况下&#xff0c;微软提供的MVC框架模板中&#xff0c;WebAPI路由是不支持Namespace参数的。这导致一些比较大型的项目&#xff0c;无法把WebApi分离到单独的类库中。 本文将提供解决该问题的方案。 微软官方曾经给出过一个关于WebAPI支持Namespace的扩展&#xff0…

[转载] Python3.X 线程中信号量的使用方法示例

参考链接&#xff1a; 示例说明Python2.x和Python3.x之间的重要区别 信号量semaphore 是一个变量&#xff0c;控制着对公共资源或者临界区的访问。信号量维护着一个计数器&#xff0c;指定可同时访问资源或者进入临界区的线程数。下面这篇文章主要给大家介绍了关于Python3.X 线…

从流程的自动化中获得最大价值的10种方式

流程自动化很好&#xff0c;如果它可以节省时间并减少错误。但是如果它不能在业务流程中“很好地契合”&#xff0c;那么会难以得到普及。问问有谁没有对语音助手感到伤脑筋。 所幸的是&#xff0c;某些最佳实践让你可以从流程自动化中获得最大价值&#xff0c;以下就是其中的1…

java中null是常量吗_C_NULL Julia中的常量

java中null是常量吗Julia| C_NULL常数 (Julia | C_NULL Constant) C_NULL is a constant of Ptr{Nothing} type in Julia programming language, it represents the null pointer value, which is used for C Null Pointer while calling external code. C_NULL是Julia编程语言…

[转载] Python京东抢购

参考链接&#xff1a; 从Python获取输入 Python京东抢购 分析其中提交信息接口的参数&#xff0c;可以成功抢购商品&#xff0c;并且可以提交订单。。。。2018年7月17日 提交信息的获取 直接提交信息对post提交分析其中的参数。 经过分析参数大多数在&#xff1a;https…

6.04 从字符串中删除不需要的字符

需求&#xff1a;删除所有的0和元音字母。 select ename,replace(replace(replace(replace(replace(ename,A,),E,),I,),O,),U,) as stripped1,sal,replace(sal,0,) stripped2from emp;转载于:https://www.cnblogs.com/liang545621/p/7518766.html

Scala分号

Scala分号 (Scala semicolons) A semicolon or semi-colon (;) is a punctuation mark in programming, it is used to separate multiple lines of code. It is common in major programming languages like C, C, Java, Pascal. In modern programming languages like Python…

[转载] python通过adb获取android手机耗电量

参考链接&#xff1a; 从Python中控制台获取输入 把开发者模式打开&#xff0c;激活 adb 调试&#xff0c;然后可以使用以下python代码获取安卓手机的耗电量 # -*- coding: utf-8 -*- import re import os def getSelectDevice(): pip os.popen(adb devices) result pip.…

ES6之主要知识点(二) 变量的解构赋值。默认值

引自http://es6.ruanyifeng.com/#docs/destructuring 数组解构赋值默认值对象解构赋值用途1.数组的解构赋值 let [a, b, c] [1, 2, 3]; let [foo, [[bar], baz]] [1, [[2], 3]]; foo // 1 bar // 2 baz // 3let [ , , third] ["foo", "bar", "baz&…

python无符号转有符号_Python | 散布符号

python无符号转有符号There are multiple types of Scatter Symbols available in the matplotlib package and can be accessed through the command marker. In this article, we will show some examples of different marker types and also present a list containing all…

[转载] 基于LSTM的股票预测模型_python实现_超详细

参考链接&#xff1a; 从Python获取输入 文章目录 一、背景二、主要技术介绍1、RNN模型2、LSTM模型3、控制门工作原理四、代码实现五、案例分析六、参数设置七、结论完整程序下载 一、背景 近年来&#xff0c;股票预测还处于一个很热门的阶段&#xff0c;因为股票市场的波动…

shell -eom_EOM的完整形式是什么?

shell -eomEOM&#xff1a;消息结尾 (EOM: End Of Message) EOM is an abbreviation of "End Of Message". EOM是“消息结尾”的缩写 。 It is an expression, which is commonly used in the Gmail platform. It is also written as Eom or eom. It is written at …

在eclipse中启动Tomcat访问localhost:8080失败项目添加进Tomcat在webapp中找不到

软件环境&#xff1a;Eclipse oxygen&#xff0c; Tomcat8.5 #在eclipse中启动Tomcat访问localhost:8080失败 在eclipse中配置tomcat后&#xff0c;打开tomcat后访问localhost:8080后无法出现登陆成功的界面,即无法出现下面的界面 在eclipse中的servers状态栏中双击tomcat&…

[转载] 【基础教程】Python input()函数:获取用户输入的字符串

参考链接&#xff1a; 从Python中控制台获取输入 input() 是 Python 的内置函数&#xff0c;用于从控制台读取用户输入的内容。input() 函数总是以字符串的形式来处理用户输入的内容&#xff0c;所以用户输入的内容可以包含任何字符。 input() 函数的用法为&#xff1a; str…

程序员简历工作模式_简历的完整形式是什么?

程序员简历工作模式简历&#xff1a;简历 (CV: Curriculum Vitae) The CV is an abbreviation of Curriculum Vitae. It is a written outline summary of a persons educational training and qualifications and his other experiences. It is an absolute profile of a cand…

[转载] Python新手写出漂亮的爬虫代码1——从html获取信息

参考链接&#xff1a; Python中从用户获取多个输入 Python新手写出漂亮的爬虫代码1 初到大数据学习圈子的同学可能对爬虫都有所耳闻&#xff0c;会觉得是一个高大上的东西&#xff0c;仿佛九阳神功和乾坤大挪移一样&#xff0c;和别人说“老子会爬虫”&#xff0c;就感觉特别…

在Scala中设置&()方法

Scala中的Set&#xff06;()方法 (The Set &() method in Scala) The &() method in the Set is used to create a new set in Scala. This new set created contains all elements from the other two sets that are common for both of the given sets i.e. new set …

[转载] python与c/c++相比有哪些优势

参考链接&#xff1a; Python输入和C, Java速度对比 理论上&#xff0c;python的确比C/C慢&#xff08;我对Java的开发没有经验&#xff0c;无法评论&#xff09;。这一点不用质疑。 C/C是编绎语言&#xff0c;直接使用的是机器指令&#xff0c;而python总是跑在的虚拟机上&am…