
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Swapping Characters of a String in C#
To swap characters of a string, use the Select method.
Firstly, let’s say our string is −
string str = "PQRQP";
Now, you need to swap every occurrence of P with Q and Q with P −
str.Select(a=> a == 'P' ? 'Q' : (a=='Q' ? 'P' : a)).ToArray();
The above replaces the characters.
Let us see the compelet code −
Example
using System; using System.Linq; public class Program { public static void Main() { string str = "PQRQP"; var res= str.Select(a=> a == 'P' ? 'Q' : (a=='Q' ? 'P' : a)).ToArray(); str = new String(res); Console.WriteLine(str); } }
Output
QPRPQ
- Related Questions & Answers
- Swapping Characters of a String in Java
- Maximum length of balanced string after swapping and removal of characters in C++
- Swapping adjacent words of a String in JavaScript
- XOR of Prime Frequencies of Characters in a String in C++
- Swapping string case using a binary number in JavaScript
- Convert characters of a string to opposite case in C++
- Regrouping characters of a string in JavaScript
- Program to equal two strings of same length by swapping characters in Python
- Print all distinct characters of a string in order in C++
- Count Unique Characters of All Substrings of a Given String in C++
- How to convert a list of characters into a string in C#?
- Convert a String into a square matrix grid of characters in C++
- C# program to convert a list of characters into a string
- Maximum Length of a Concatenated String with Unique Characters in C++
- Swapping of subranges from different containers in C++
Advertisements