. The
a specified portion of the invoking string. Because a new
string
162
P a r t I :
T h e C # L a n g u a g e
contains the substring, the original string is unaltered, and the rule of immutability is still
intact. The form of
Substring( )
that we will be using is shown here:
string Substring(int
start
, int
len
)
Here,
start
specifies the beginning index, and
len
specifies the length of the substring.
Here is a program that demonstrates
Substring( )
and the principle of immutable strings:
// Use Substring().
using System;
class SubStr {
static void Main() {
string orgstr = "C# makes strings easy.";
// construct a substring
string substr = orgstr.Substring(5, 12);
Console.WriteLine("orgstr: " + orgstr);
Console.WriteLine("substr: " + substr);
}
}
Here is the output from the program:
orgstr: C# makes strings easy.
substr: kes strings
As you can see, the original string
Do'stlaringiz bilan baham: