cs321 Winter 2023 Lecture 4 Strings
cs321 Winter 2023 Lecture 4 Strings
What is a
A template for creating objects class in
OOP?
Describes methods and data
Classes in C#
• System.String is a class
• System.Object is a class
• System.Type is also a class
• A class is a kind of data type
• Classes describe the data and methods contained by “instances” of the class
• Instances of a class are values, known as objects
• Classes can derive from (inherit) another class (at most one)
• Classes without an explicit base class, derive from System.Object
Example of a Class Hierarchy
System.Object
System.String
Variables
• All variables have a fixed type determined at compile-time
• A variable refers to an instance of a type (or null)
• Variables may be initialized when declared (best practice)
• Variables may be reassigned (use sparingly)
• Variables cannot be assigned a value of an incorrect type
Parameters and Arguments
Function parameters are a special kind of variable (also called “formal arguments”)
Indicates that the accepted type of parameters, local variables, and return types is treated as a
System.Object
01 02 03 04
String.IndexOf String.Substring String.Split String.Join
String.IndexOf()
String.IndexOf Demo
String.Substring
String.Substring Demo
String.Split
String.Split() Demo
Params: Variable Length Arguments
The params keywords means that I can do this instead as well: notice no array.
String.Join
String.Join Demo
Instance methods have the form
“expression.FunctionName(args)”
Invoking
Instance
versus Static Static method have the form
Methods “typename.FunctionName(args)”
WHY IS STRING.JOIN STATIC?
CONVERTING BYTES TO/FROM
STRINGS
BIT
CONVERTER
DOES NOT
WORK ON
STRINGS?!
string value = "\u00C4 \uD802\u0033 \u00AE"; byte[] bytes= System.Text.Encoding.UTF8.GetBytes(value);
System.Text.Encoding.UTF8.GetBytes(value);
Remember Encodings?
We need to choose one, such as
System.Text.Encoding.UTF8.GetBytes()
System.Text.Encoding.UTF16.GetBytes()
System.Text.Encoding.ASCIIEncoding.GetBytes()
String Constructors
String(Char, Int32) - Initializes a new instance of the String class to the value indicated by a
specified Unicode character repeated a specified number of times.
String(Char[]) - Initializes a new instance of the String class to the Unicode characters
indicated in the specified character array.
String Operators
+= String
+ String
concatenation == Equality != Inequality
concatenation
and assignment
They have a Length property
So how do
you build String interpolation
expression
Concatenation
strings?
From an array of chars
What about memory?
• Do we care?
• If two strings return “equal” and have the same hash-code?
• They are effectively equal
• You could call “Object.ReferenceEquals()”, but don’t.
Indexers
An indexer can accept any type of parameters (like an int, string, object.)
String Formatting
• Before string interpolation we had string
formatting routines
• Like a safe and powerful version of the C
function sprintf().
• String.Format()
String Formatting
Formatting with String interpolation
The Null Literal
• The null keyword represents a reference that does not refer to an object.
• It has a special type (called the null type) but can be cast to any reference type
• Reference variables are assigned null by default
• In other words it means “no value”
• Different from the empty string (“”)
NullReferenceException
Checking if Strings are Null or Empty
STRING
QUERIES
Strings implement IEnumerable
https://learn.microsoft.com/en-us/dotnet/api/system.collections.ienumerable.getenumerator?view=net-7.0
Interview Questions with Strings
• Get all duplicated characters in a string.
• Get all unique characters in a string.
• Reverse a string.
• Reverse each word in a string
• Get the word count in a string
• Check if a string is a palindrome or not
• Check max occurrence of a character in the string.
• Get all possible substring in a string.
• Get the first char of each word in capital letter
• Check if two strings are anagrams
• Remove duplicated characters
• Check if a function has all unique characters
Review
• Do all variables have types?
• How is the type of an implicitly typed variable declaration determined?
• What does the “var” keyword indicate?
• Can I access methods specific to a string (like Length) on a variable of type “object”?
• Can a variable of type “object” refer to a “string” object?
• How can I determine the run-time type of an object?
• What is an instance of a class called?
• What does System.String inherit from?
• Casting from System.String to System.Object is an upcast or downcast?
• Are upcasts explicit or implicit?
• Can I change the type of a value?
• Are types valid expressions?
Next Class
• Collections
• Building our First Class