Chapter 3

Pulling Strings

IN THIS CHAPTER

check Pulling and twisting a string with C#

check Matching searching, trimming, splitting, and concatenating strings

check Parsing strings read into the program

check Formatting output strings manually or using the String.Format() method

For many applications, you can treat a string like one of the built-in value-type variable types such as int or char. Certain operations that are otherwise reserved for these intrinsic types are available to strings:

Int i = 1; // Declare and initialize an int.string s = "abc"; // Declare and initialize a string.

In other respects, as shown in the following example, a string is treated like a user-defined class (Book 2 discusses classes):

string s1 = new String();string s2 = "abcd";int lengthOfString = s2.Length;

Which is it — a value type or a class? In fact, String is a class for which C# offers special treatment because strings are so widely used in programs. For example, the keyword string is synonymous with the class name String, as shown in this bit of code:

String s1 = "abcd"; // Assign a string literal to a String ...

Get C# 7.0 All-in-One For Dummies now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.