
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
String slicing in C# to rotate a string
Let’s say our string is −
var str = "welcome";
Use the substring() method and the following, if you want to rotate only some characters. Here, we are rotating only 2 characters −
var res = str.Substring(1, str.Length - 1) + str.Substring(0, 2);
The following is the complete code −
Example
using System; public class Program { public static void Main() { var str = "welcome"; Console.WriteLine("Original String = "+str); var res = str.Substring(1, str.Length - 1) + str.Substring(0, 2); Console.WriteLine("Rotating two characters in the String: "+res); } }
Output
Original String = welcome Rotating two characters in the String: elcomewe
- Related Articles
- String slicing in Python to rotate a string
- Rotate String in Python
- String slicing in Python to check if a can become empty by recursive deletion
- Program to rotate a string of size n, n times to left in Python
- How to split a string with a string delimiter in C#?
- How to copy a String into another String in C#
- How to initialize a string to an empty string in C#?
- Object Slicing in C++
- How to split a string into elements of a string array in C#?
- Check if a string contains a sub-string in C++
- Equals(String, String) Method in C#
- Convert a String to Uppercase in C
- How to create a comma separated string from a list of string in C#?
- Reverse a string in C/C++
- Replace part of a string with another string in C++

Advertisements