Замена символа в строке - Visual Basic .NET
Формулировка задачи:
Найти и заменить определенный символ в строке, введенной с клавиатуры. Программа должна запрашивать заменяемый и заменяющий символы, а также подтверждение каждой замены символа с сообщением о номере его позиции в строке.
Не знаю как заменить определенный символ. Прошу помощи. Заранее спасибо.
Листинг программы
- Sub Main()
- Console.Write("Введите строку: ")
- Dim str As String = Console.ReadLine(), replaceable, replacing As Char, querty As Boolean
- Console.Write("Какой символ заменять: ")
- replaceable = Console.ReadLine()
- Console.Write("На какой символ заменять: ")
- replacing = Console.ReadLine()
- For i = 0 To Len(str) - 1
- 'Здесь должна быть проверка на совпадение символа
- Console.Write("Заменить символ №" & i + 1 & " на " & replacing & "? ")
- querty = Console.ReadLine()
- If querty = True Then
- ' Здесь замена символа
- End If
- Next
- Console.WriteLine(Replace(str, replaceable, replacing, , 1))
- Console.ReadLine()
- End Sub
Решение задачи: «Замена символа в строке»
textual
Листинг программы
- Sub Main()
- Console.Write("Введите строку: ")
- Dim str As String = Console.ReadLine(), replaceable, replacing As Char, number As Integer, querty As String
- Console.Write("Какой символ заменять: ")
- replaceable = Console.ReadLine()
- Console.Write("На какой символ заменять: ")
- replacing = Console.ReadLine()
- Dim result = ReplaceWithDialog(str, replaceable, replacing)
- Console.WriteLine("Строка после замены: {0}", result)
- Console.ReadKey()
- End Sub
- Private Function ReplaceWithDialog(s As String, replaceable As Char, replacing As Char) As String
- Dim result As String = s
- Dim index As Integer = result.IndexOf(replaceable)
- While index <> -1
- Console.Write(String.Format("Заменить символ №{0} на '{1}'? (Enter — да, Esc — нет): ", index + 1, replacing))
- Dim cki = Console.ReadKey(False)
- If cki.Key = ConsoleKey.Enter Then
- result = String.Format("{0}{1}{2}", result.Substring(0, index), replacing, s.Substring(index + 1))
- End If
- Console.WriteLine()
- Console.WriteLine(result)
- index = result.IndexOf(replaceable, index + 1)
- End While
- Return result
- End Function
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д