博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
scala反转字符串_Scala程序反转字符串
阅读量:2533 次
发布时间:2019-05-11

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

scala反转字符串

反转字符串 (Reversing a string)

Logically, reversing is swapping the values from index 1 with index n, index 2 with index n-1, and so on.

从逻辑上讲,反向是将索引1中的值与索引n交换,将索引2中的值与索引n-1交换,依此类推。

So, if the string is "IncludeHelp", then the reverse will be "pleHedulcnI".

因此,如果字符串为“ IncludeHelp” ,则反向为“ pleHedulcnI”

Example:

例:

Input:    String: "IncludeHelp"    Output:    Reversed string: "pleHedulcnI"

程序在Scala中反转字符串 (Program to reverse a string in Scala)

object myObject {
def reverseString(newString: String): String = {
var revString = "" val n = newString.length() for(i <- 0 to n-1){
revString = revString.concat(newString.charAt(n-i-1).toString) } return revString } def main(args: Array[String]) {
var newString = "IncludeHelp" println("Reverse of '" + newString + "' is '" + reverseString(newString) + "'") }}

Output

输出量

Reverse of 'IncludeHelp' is 'pleHedulcnI'

Another method will be to convert the string into a list and then reversing the list and the converting in back to the string.

另一种方法是将字符串转换为列表,然后反转列表并将其转换回字符串。

Program:

程序:

object myObject {
def main(args: Array[String]) {
var newString = "IncludeHelp" var revString = newString.foldLeft(List[Char]()){(x,y)=>y::x}.mkString("") println("Reverse of '" + newString + "' is '" + revString + "'") }}

Output

输出量

Reverse of 'IncludeHelp' is 'pleHedulcnI'

翻译自:

scala反转字符串

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

你可能感兴趣的文章
《JavaScript总结》apply、call和bind方法
查看>>
Alpha 冲刺 (5/10)
查看>>
递归和二分法
查看>>
vs命令行参数说明
查看>>
C++Builder调整TreeView和ListView间距
查看>>
205. Isomorphic Strings
查看>>
SynchronousQueue
查看>>
JQuery常用函数及功能小结
查看>>
POJ 2653 Pick-up sticks 线段相交
查看>>
PKU JudgeOnline 题目分类
查看>>
网站报错Access denied for user 'root'@'localhost' -问题排查续
查看>>
字符串处理sdut 2411
查看>>
javascript之进阶
查看>>
多个窗体间控件的调用
查看>>
flex伸缩布局
查看>>
第三方类库添加强签名
查看>>
通过日期获取星期几,通过日期获取凌晨、上午、中午、下午、晚上
查看>>
Java知多少(17)强调一下编程风格
查看>>
CSS3绘制的军曹giroro卡通图像
查看>>
【转】django 与 vue 的完美结合 实现前后端的分离开发之后在整合
查看>>