Jump to content

C# Strings

- - - - -

  • Please log in to reply
3 replies to this topic

#1
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
C# Strings

Strings are a programmatic representation of text. For various operations it is useful to think of strings as character arrays as they are usually implemented as.

Creating Strings

Creating a string is simple. Have a look at this code:

 string s = "test";

What text we want the string to hold is put in double quotes and assigned to a variable using the assignment operator. Simple. It is important to note that once a string is created it is immutable. If you want to change the string you have to make a new string. We will later look at how to modify strings without creating a new string.

We will now consider various string functions.

String Length


It is common to want to know how long a string is. For this we simply use the Length method of the string class.
Example:
string s = "test"; 
Console.WriteLine(s.Length()); 
The output of the above code will be:

Quote

4

IndexOf

The IndexOf method returns the index of the first instance of a character in a string or -1 if it does not exist.

Example:
 string s = "test"; 
Console.WriteLine(s.IndexOf('t'));
The output of this code will be:

Quote

0

What if we want to know the last index of a character? For this we make use of the LastIndexOf method as follows:
 string s = "test";
 Console.WriteLine(s.LastIndexOf('t'));

The output is:

Quote

3

If we want the first occurrence after the first occurrence we would write:

 
string s = "test"; 
Console.WriteLine(s.IndexOf('t',1));

This code starts the search for t at index 1 in s.
I’m going to leave a challenge here: how would you find all instances of t in s using the IndexOf method?
See if you can find out how to do the same thing using LastIndexOf.

Replace

This method replaces all instances of one character with another character.
Note: it doesn’t actually replace the character in the original string but produces a new string.

Example:
 string s = "test"; 
s = s.Replace('t', 'r');
Console.WriteLine(s);  

The output of this code is:

Quote

resr

A second version of this method exists which lets you replace all occurrences of a substring with another substring. Example:

 
string s = "test";
 
s = s.Replace("es", "on");
Console.WriteLine(s);
s = s.Replace("t", "x");
Console.WriteLine(s);
 

The first replace takes the string “test” and replaces all occurrences of “es” with “on” producing the string “tont”. The second replace takes the string “tont” and replaces all occurrences of “t” with “x” producing the string “xonx”. In the execution of this program, s is never changed but three new strings are created. If you are going to be changing characters often it is best not to use replace but to use StringBuilders which we will look at soon.

Substring

One of the most useful string method is the substring method. This lets you extract a portion of the string into a new string. To use this method you need to know the start index of where you want to start copying from and optionally the number of characters you want to copy. Like arrays, strings start at index 0 so we need to keep that in mind when using this method.

Examples:

 
string s = "welcome to codecall";
Console.WriteLine(s.Substring(1));

This example copies the entire string from index 1 to the last character in the string. The output is then:

Quote

elcome to codecall

If we only want to copy a portion of the string we use a second implementation of this method.

Example:

string s = "welcome to codecall";
Console.WriteLine(s.Substring(2,5));

This code produces the string starting at index 2. The second parameter tells us how many characters to take. In this case we are going to copy 5 characters.

The output is:

Quote

lcome

StartsWith

This method simply takes a string and returns true if the object this method is being called on starts with that string. Example:

string s = "test";
Console.WriteLine(s.StartsWith("t"));  

Output is:

Quote

True

EndsWith

This method simply takes a string and returns true if the object this method is being called on ends with that string. Example:

  
string s = "test";
Console.WriteLine(s.EndsWith("ast")); 
 

Output is:

Quote

False


#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Okay, double-quotes are used for defining strings.

In C++, double-quotes make a pointer to a string that has all that stuff between the quotes, while single-quotes make a literal value (not a pointer to that value).


What do single-quotes do in C#?

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
Single-quotes are used to define a character. Example: char c = 'x'; Which is true in C++ also...

so you are saying in C++ string s = 'chili'; is valid?

#4
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I don't know; but maybe there's some C++ person on this forum who could answer that?

The way I usually would do something like that is:
char s[512]; 

strcpy(s, "chili"); 





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users