kotlin 字符串去空格_Kotlin程序从字符串中删除所有空格

kotlin 字符串去空格

Given a string, we have to remove all whitespaces from it.

给定一个字符串,我们必须从中删除所有空格。

Example:

例:

    Input:
    string = "Hello world!"

    Output:
    "Helloworld!"

在Kotlin中从字符串中删除所有空格的程序 (Program to remove all whitespaces from the string in Kotlin)

In this program, at first, we are reading a string and them replacing all whitespaces using the replace() function.

首先,在此程序中,我们正在读取一个字符串,并使用replace()函数替换所有空白。

package com.includehelp.basic

import java.util.*

//Main Function, entry Point of Program
fun main(args: Array<String>) {
    // InputStream to get Input
    val scanner = Scanner(System.`in`)

    //Input String
    print("Enter String : ")
    var str = scanner.nextLine()

    //Replace character with Empty String
    val newStr=str.replace("\\s".toRegex(),"")

    //Print String after Replacement
    println("String after Removed WhiteSpaces : $newStr ")
}

Output

输出量

Run 1:
Enter String : Hello includehelp how are you ?
String after Removed WhiteSpaces : Helloincludehelphowareyou?
---
Run 2:
Enter String : Covid -19 [pandimic spread all over the world
String after Removed WhiteSpaces : Covid-19[pandimicspreadallovertheworld


翻译自: https://www.includehelp.com/kotlin/remove-all-whitespaces-from-the-string.aspx

kotlin 字符串去空格

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值