Q:

Kotlin program to remove all occurrences of a character in a string

belongs to collection: Kotlin String Programs

0

Given a string and a character, we have to remove all occurrences of the character in given string.

Example:

    Input:
    string = "includeHelp Delhi"

    Output:
    String after removing character : "includeelp Deli"

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to remove all occurrences of a character in a string in Kotlin

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()

    //Input Character to Remove from String
    print("Enter Character : ")
    val c = scanner.next()[0]


    //Replace character with Empty String
    val newStr=str.replace(c.toString(),"",ignoreCase = true)


    //Print String after Replacement
    println("String after removing character : $newStr ")
}

Output

Run 1:
Enter String : includeHelp Delhi
Enter Character : h
String after removing character : includeelp Deli
---
Run 2:
Enter String : corona virus global death rate
Enter Character : o
String after removing character : crna virus glbal death rate

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin program to reverse each word in string... >>
<< Kotlin program to find first non-repeating charact...