Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Begin your success journey!

Sign Up using
Full name *
Email *
Password *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy .

Welcome back!

Log In using
Email *
Password *

Strings in C#

In this article, we will learn about Strings in C#.

A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects. There is no null-terminating character at the end of a C# string; therefore a C# string can contain any number of embedded null characters (‘\0’). The Length property of a string represents the number of Char objects it contains, not the number of Unicode characters. To access the individual Unicode code points in a string, use the StringInfo object.


Declaring and Initializing Strings

In C#, string and String are equivalent. You can declare and initialize strings in various ways, as shown in the following example:


// Declare without initializing.
string message1;

// Initialize to null.
string message2 = null;

// Initialize as an empty string.
// Use the Empty constant instead of the literal "".
string message3 = System.String.Empty;


// Use System.String if you prefer.
System.String greeting = "Hello World!";

// In local variables (i.e. within a method body)
// you can use implicit typing.
var temp = "I'm still a strongly-typed System.String!";

// Use a const string to prevent 'message4' from
// being used to store another string value.
const string message4 = "You can't get rid of me!";

// Use the String constructor only when creating
// a string from a char[].
char[] letters = { 'A', 'B', 'C' };
string alphabet = new string(letters);

Immutability of Strings

String objects are immutable: they cannot be changed after they have been created. All of the String methods and C# operators that appear to modify a string actually return the results in a new string object. In the following example, when the contents of s1 and s2 are concatenated to form a single string, the two original strings are unmodified. The += operator creates a new string that contains the combined contents. That new object is assigned to the variable s1, and the original object that was assigned to s1 is released for garbage collection because no other variable holds a reference to it.

Therefore, different operations on strings such as concantention are of O(n) time complexity, where n is the length of the strings.


Important String Methods

  • Compare(String, String): It is used to compares two specified String objects. It returns an integer that indicates their relative position in the sort order.
  • Contains(String): It is used to return a value indicating whether a specified substring occurs within this string.
  • Equals(String, String): It is used to determine that two specified String objects have the same value.
  • IndexOf(String): It is used to report the zero-based index of the first occurrence of the specified string in this instance.
  • Insert(Int32, String): It is used to return a new string in which a specified string is inserted at a specified index position.
  • Split(Char[]): It is used to split a string into substrings that are based on the characters in an array.
  • ToCharArray(): It is used to copy the characters in this instance to a Unicode character array.
  • ToLower(): It is used to convert String into lowercase.
  • ToUpper(): It is used to convert String into uppercase.
  • Substring(Int32): It is used to retrieve a substring from this instance. The substring starts at a specified character position and continues to the end of the string.

We can use array indexing for accessing characters of a string, we do not need a separate method for it in C#.


Task

In the editor below, perform the different tasks as directed.

Start solving Strings in C# on Interview Code Editor
Hints
  • Solution Approach
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Graduation Year *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test