How to change different substrings in a string?

number of distinct substrings in a string
replace a substring in a string in c
minimum changes to a string to make all substrings distinct
replace a substring in a string python
substring in c
how to replace a substring with another substring in c++
replace a substring in a string in c++
given a string reduce it in such a way that all of its substrings are distinct

I have a file with a certain number of lines. I need to find all double letters and change the second letter to the one that corresponds to it in the map. Case of replaced letters should be kept. Example: "kotlIn is a functional programming Language" should become "kotlYn is a functionol programmyng Longuage"

fun changeChar(inputFileName: String, outputFileName: String) {
    val outputStream = File(outputFileName).bufferedWriter()
    val charsRegex = "([klmn][aei])".toRegex(setOf(RegexOption.IGNORE_CASE))
    val validChars = mapOf('a' to 'o', 'e' to 'u', 'i' to 'y')
    File(inputFileName).forEachLine { line ->
        val sb = StringBuilder(line).replace(charsRegex, /*here is my difficulty*/)

I spent many hours, but could not find solutions on the Internet and the standard Kotlin library.

Another idiomatic solution would be to transform the matched string using the replace(...) { ... } overload that accepts a lambda to handle each MatchResult:

val charsRegex = "[klmn][aei]".toRegex(RegexOption.IGNORE_CASE)
val validChars = mapOf('a' to 'o', 'e' to 'u', 'i' to 'y')

val result = line.replace(charsRegex) {
    val vowel = it.value[1]
    val validLetter = validChars.getValue(vowel.toLowerCase())
    val validCaseLetter = if (vowel.isLowerCase()) validLetter else validLetter.toUpperCase()
    it.value.take(1) + validCaseLetter

(runnable sample)

Minimum changes to a string to make all substrings distinct , Given a string, find minimum number of changes to it so that all substrings of the string become distinct. Examples : Input : str = "aab" Output : 1 If we change one  Generalizing on rotmax's answer, here is a full solution to search & replace all instances in a string. If both substrings are of different size, the substring is replaced using string::erase and string::insert., otherwise the faster string::replace is used.

Without any regex:

val validChars = mapOf('a' to 'o', 'e' to 'u', 'i' to 'y')
val chars = { it.key.toUpperCase() to it.value.toUpperCase() }.toMap())

var pattern = "klmn"
pattern =

val str = "kotlIn is a functional programming Language"
val result = str.mapIndexed { i, c ->
    if (i > 0) { if (pattern.contains(str[i -1])) chars.getOrDefault(c, c) else c } else c

will print:

kotlYn is a functionol programmyng Longuage

Replace two substrings (of a string) with each other, Replace two substrings (of a string) with each other. Given 3 strings S, A and B. The task is to replace every sub-string of S equal to A with B and every sub-string   String substring() method variants. There are two ways we can use the substring() method – 1. When we pass only the starting index: String substring(int beginIndex) Returns the substring starting from the specified index i.e beginIndex and extends to the character present at the end of the string. For example – "Chaitanya".substring(2) would return "aitanya". The beginIndex is inclusive, that is why the character present at the index 2 is included in the substring.

Try this.

fun main(args: Array<String>) {
    val str = "kotlIn is a functional programming Language"
    val validChars = mapOf("a" to "o", "e" to "u", "i" to "y" )
    val charsRegex = "(?=(.))([klmn][aei])(?<=(.))".toRegex(setOf(RegexOption.IGNORE_CASE))
    val found = charsRegex.findAll(str)
    var newStr= str
    found.forEach { f ->
        val m = f.value.toLowerCase()
        val idx = f.range
        val k = m.substring(1)
        val repStr = m.substring(0,1) + validChars.get(k)?.toUpperCase()
        newStr= newStr.replaceRange(idx,repStr)
       // println("$m found at indexes: $idx  ${validChars.get(k)} ${newStr}" )
    println( "NewStr = ${newStr}")


NewStr = kotlYn is a functionOl programmYng lOnguage

PS: I'm not that good at Kotlin, but I think you can tweak it a little to get the accurate results.

Manipulating Characters in a String (The Java™ Tutorials , The String class has a number of methods for examining the contents of strings, finding characters or substrings within a string, changing case, and other tasks. Replace two substrings (of a string) with each other; Different substrings in a string that start and end with given strings; Count of substrings of a binary string containing K ones; Split the string into substrings using delimiter; Sum of all substrings of a string representing a number | Set 1; Number of even substrings in a string of digits; Number of substrings of one string present in other

Replace strings in Python (replace, translate, re.sub, re.subn), Replace multiple different substrings. When replacing multiple different strings with the same string, use the regular expression described later. Count the number of vowels occurring in all the substrings of given string; C program to count number of vowels and consonants in a String; Longest substring with count of 1s more than 0s; First substring whose reverse is a word in the string; Shortest substring of a string containing all given words; Count distinct elements in an array

Replace substrings between start and end points, The newText argument can have a different number of characters than the substring it replaces. If str is a string array or a cell array of character vectors, then   Starting in R2017a, you can create strings using double quotes. str = [ "the quick brown fox" ; "and the lazy dog"] str = 2x1 string "the quick brown fox" "and the lazy dog". Replace a substring in each element of the array. newStr = strrep (str, 'the', 'a') newStr = 2x1 string "a quick brown fox" "and a lazy dog".

Find and replace substrings - MATLAB strrep, If any input argument is a nonscalar string array or cell array of character vectors, then the other input arguments must have compatible sizes. Examples. collapse  Important points: The index of the first character is 0 (not 1). If pos is equal to the string length, the function returns an empty string. If pos is greater than the string length, it throws out_of_range. If this happen, there are no changes in the string. If for the requested sub-string len is

String Hashing, Determine the number of different substrings in a string Converting a→0 is not a good idea, because then the hashes of the strings a, aa, aaa, … all evaluate  Substr() normally (i.e. PHP and Perl) works this way: s = Substr(s, beginning, LENGTH) So the parameters are beginning and LENGTH. But Python's behaviour is different; it expects beginning and one after END (!). This is difficult to spot by beginners. So the correct replacement for Substr(s, beginning, LENGTH) is. s = s[ beginning : beginning + LENGTH]

  • This is not what i need. I need the value to change only at the letter after klmn. Like in my pattern "([klmn][aei])". And then each aei symbol was replaced with the corresponding one in the map val validChars = mapOf('a' to 'o', 'e' to 'u', 'i' to 'y')