Download presentation
Presentation is loading. Please wait.
Published byTabitha Whitehead Modified over 9 years ago
1
C# Strings 1 CNS 3260 C#.NET Software Development
2
C# Strings 2 System.String string maps to System.String string maps to System.String Strings are Strings are reference-type objectsreference-type objects Strings are immutableStrings are immutable Strings are sealedStrings are sealed Strings have properties and methods Strings have properties and methods Strings contain 16-bit Unicode characters Strings contain 16-bit Unicode characters
3
C# Strings 3 Immutability of a string String methods do not modify the string, they return a new string String methods do not modify the string, they return a new string string str = “one two three”; str.Replace(“one”, “1”); str = str.Replace(“one”, “1”); Wrong way Right way
4
C# Strings 4 Strings are Reference-Types string s1 = null; string reference string s2 = “”; string reference string object contains “” note that: s1 != s2 String variables can be null String variables can be null null is not the same as “” (the empty string) null is not the same as “” (the empty string)
5
C# Strings 5 Underlying Characters Escape sequences Escape sequences \a- bell\a- bell \b- backspace\b- backspace \t- tab\t- tab \r- carriage return\r- carriage return \n- line feed (new-line)\n- line feed (new-line) \\- backslash\\- backslash \”- double quote\”- double quote Literal string Literal string preceded by ‘@’preceded by ‘@’ needs no escape charactersneeds no escape characters string s1 = @”C:\Demo”; string s2 = “C:\\Demo”;
6
C# Strings 6 Interfaces System.String System.String IComparableIComparable ICloneableICloneable IConvertibleIConvertible IEnumerableIEnumerable
7
C# Strings 7 Enumerating through a string Contained type is ‘char’ Contained type is ‘char’ string str = “something, something, something”; foreach(char ch in str) { // do work here }
8
C# Strings 8 String static Members Empty Empty Compare() Compare() CompareOrdinal() CompareOrdinal() Concat() Concat() Copy() Copy() Format() Format() Intern() Intern() IsInterned() IsInterned() Join() Join()
9
C# Strings 9 string.Empty static property equivalent to “” static property equivalent to “” string str = string.Empty; // means string str = “”;
10
C# Strings 10 Compare and Concatenation string.Compare(string s1, string s2) string.Compare(string s1, string s2) Returns less-than zero if s1 is lexicographically less-than s2Returns less-than zero if s1 is lexicographically less-than s2 Returns greater-than zero if it’s greaterReturns greater-than zero if it’s greater Returns zero if they are the sameReturns zero if they are the same string.CompareOrdinal string.CompareOrdinal Compares the numeric value of each underlying charCompares the numeric value of each underlying char string.Concat(params object[] strings) string.Concat(params object[] strings) Calls ToString on each object and concatenates themCalls ToString on each object and concatenates them
11
C# Strings 11 string + and == operators + and == are also defined operators + and == are also defined string s1 = “1”, s2 = “2”; string s3 = s1 + s2; s3 += “hahahaha”; if(s1 == s2) { // do something } if(s1 != s2) { // do something }
12
C# Strings 12 string.Format() string.Format(string format, params object[] args) string.Format(string format, params object[] args) format is the string to perform the formatting onformat is the string to perform the formatting on format may contain zero or more embedded format itemsformat may contain zero or more embedded format items format items have the following structure:format items have the following structure: {index[,alignment][:formatString]} {index[,alignment][:formatString]} to create a format item, you must have at least “{index}” to create a format item, you must have at least “{index}” args is a list of parameters that will be inserted into the format stringargs is a list of parameters that will be inserted into the format string decimal price = 5.99; string output = string.Format( “Price: {0,-5:C}”, price );
13
C# Strings 13 Format Items {index[,alignment][:formatString]} {index[,alignment][:formatString]} index refers to the parameter in the params array index refers to the parameter in the params array it is 0-basedit is 0-based alignment alignment requires the comma if usedrequires the comma if used is an integer specifying the minimum widthis an integer specifying the minimum width negative values mean left-justify the valuenegative values mean left-justify the value positive values mean right-justify the valuepositive values mean right-justify the value formatString formatString requires the colon if usedrequires the colon if used is sent to the parameter if it implements IFormattableis sent to the parameter if it implements IFormattable (See StringFormat Demo)
14
C# Strings 14 IFormattable Overloads the ToString() method Overloads the ToString() method ToString(string format, IFormatProvider formatProvider) Available IFormatProvider classes are: Available IFormatProvider classes are: NumberFormatInfoNumberFormatInfo DateFormatInfoDateFormatInfo
15
C# Strings 15 Back to string.Format() If the parameter does not accept the stringFormat passed, a FormatException is thrown If the parameter does not accept the stringFormat passed, a FormatException is thrown Items that are not IFormattable will always throw if passed a stringFormatItems that are not IFormattable will always throw if passed a stringFormat If the parameter is not IFormattable, it ignores the stringFormat field If the parameter is not IFormattable, it ignores the stringFormat field If you need to use ‘{‘ or ‘}’ in your string, double them up to escape them: If you need to use ‘{‘ or ‘}’ in your string, double them up to escape them: string str = string.Format(“{{ {0} }}”, 555);
16
C# Strings 16 string.Join() string.Join(string separator, string[] values) string.Join(string separator, string[] values) joins each of the strings in values separated by separatorjoins each of the strings in values separated by separator string str = string.Join(“:”, new string[] {“1”,”2”,”3”,”4”}); (See StringManipulation Demo)
17
C# Strings 17 Last 2 Static Members Literal strings are stored in a table called the Intern Pool Literal strings are stored in a table called the Intern Pool saves storagesaves storage speeds up some operationsspeeds up some operations Intern(string str) Intern(string str) if str is already interned, the system reference is returnedif str is already interned, the system reference is returned if str is not interned, it is added to the pool and the new reference is returnedif str is not interned, it is added to the pool and the new reference is returned IsInterned(string str) IsInterned(string str) if str is already interned, the system reference is returnedif str is already interned, the system reference is returned if str is not interned, a null reference is returnedif str is not interned, a null reference is returned
18
C# Strings 18 Switching on strings The Intern Pool should *theoretically* make switching on strings faster than doing multiple if-else statements The Intern Pool should *theoretically* make switching on strings faster than doing multiple if-else statements but...but... Results from the StringSwitchBench Demo show otherwise: Results from the StringSwitchBench Demo show otherwise: (See StringSwitchingBench Demo)
19
C# Strings 19 Instance Members Indexer Indexer Length Length EndsWith() EndsWith() Equals() Equals() IndexOf() IndexOf() IndexOfAny() IndexOfAny() Insert() Insert() LastIndexOf() LastIndexOf() LastIndexOfAny() LastIndexOfAny() PadLeft() PadLeft() PadRight() PadRight() Remove() Remove() Replace() Replace() Split() Split() StartsWith() StartsWith() Substring() Substring() ToLower() ToLower() ToUpper() ToUpper() Trim() Trim() TrimStart() TrimStart() TrimEnd() TrimEnd()
20
C# Strings 20 String Indexer gets the char at the specified location gets the char at the specified location readonly (no set) readonly (no set) string str = “hello world” char ch = str[4];
21
C# Strings 21 String Equals Equals(string str) Equals(string str) tests the equality of the value of the calling string and strtests the equality of the value of the calling string and str (See InternPool Demo)
22
C# Strings 22 EndsWith() and StartsWith() EndsWith(string str) EndsWith(string str) returns true if the calling string ends with strreturns true if the calling string ends with str StartsWith(string str) StartsWith(string str) returns true if the calling string starts with strreturns true if the calling string starts with str
23
C# Strings 23 Searching strings IndexOf (string substr) IndexOf (string substr) Returns the integer index of the first occurrence of substr in the calling stringReturns the integer index of the first occurrence of substr in the calling string LastIndexOf (string substr) LastIndexOf (string substr) Returns the integer index of the last occurrence of substr in the calling stringReturns the integer index of the last occurrence of substr in the calling string IndexOfAny (char[] search) IndexOfAny (char[] search) Returns the integer index of the first occurrence of any of the characters in searchReturns the integer index of the first occurrence of any of the characters in search LastIndexOfAny (char[] search) LastIndexOfAny (char[] search) Returns the integer index of the last occurrence of any of the characters in searchReturns the integer index of the last occurrence of any of the characters in search **Each of these returns -1 if the substring or character cannot be found **Each of these returns -1 if the substring or character cannot be found
24
C# Strings 24 Insert, Remove, Replace Insert(int position, string str) Insert(int position, string str) Returns a new copy of the calling string with str inserted at positionReturns a new copy of the calling string with str inserted at position Remove(int start, int count) Remove(int start, int count) Returns a new copy of the calling string with the specified section removedReturns a new copy of the calling string with the specified section removed Replace(string oldValue, string newValue) Replace(string oldValue, string newValue) Replace(char oldValue, char newValue) Replace(char oldValue, char newValue) Returns a new copy of the calling string with all instances of oldValue replaced with newValueReturns a new copy of the calling string with all instances of oldValue replaced with newValue
25
C# Strings 25 Substring() Substring(int start, int count) Substring(int start, int count) Returns a new string with the substring of the calling string from start for count charactersReturns a new string with the substring of the calling string from start for count characters Substring(int start) Substring(int start) Returns a new string with the substring of the calling string from start to the end of the stringReturns a new string with the substring of the calling string from start to the end of the string
26
C# Strings 26 Padding strings PadLeft(int width) PadLeft(int width) PadLeft(int width, char padChar) PadLeft(int width, char padChar) If the string is shorter than width:If the string is shorter than width: Returns a new copy of the calling string, padding the left side with padChar Returns a new copy of the calling string, padding the left side with padChar If no padChar is specified, space is used If no padChar is specified, space is used PadRight(int width) PadRight(int width) PadRight(int width, char padChar) PadRight(int width, char padChar) If the string is shorter than width:If the string is shorter than width: Returns a new copy of the calling string, padding the right side with padChar Returns a new copy of the calling string, padding the right side with padChar If no padChar is specified, space is used If no padChar is specified, space is used
27
C# Strings 27 Manipulating Case ToUpper() ToUpper() Returns a new copy of the calling string with all letter characters set to upper-caseReturns a new copy of the calling string with all letter characters set to upper-case ToLower() ToLower() Returns a new copy of the calling string with all letter characters set to lower-caseReturns a new copy of the calling string with all letter characters set to lower-case
28
C# Strings 28 Trimming Trim() Trim() Returns a new copy of the calling string with all white space removed from the beginning and endReturns a new copy of the calling string with all white space removed from the beginning and end Trim(params char[] trimChars) Trim(params char[] trimChars) Returns a new copy of the calling string with all characters in the trimChars set removed from the beginning and endReturns a new copy of the calling string with all characters in the trimChars set removed from the beginning and end TrimEnd(params char[] trimChars) TrimEnd(params char[] trimChars) Returns a new copy of the calling string with all characters in the trimChars set removed from the endReturns a new copy of the calling string with all characters in the trimChars set removed from the end TrimStart(params char[] trimChars) TrimStart(params char[] trimChars) Returns a new copy of the calling string with all characters in the trimChars set removed from the beginningReturns a new copy of the calling string with all characters in the trimChars set removed from the beginning
29
C# Strings 29 Split() The opposite of string.Join() The opposite of string.Join() Split(params char[] delimiters) Split(params char[] delimiters) Returns an array of stringsReturns an array of strings Splits the calling string each time it finds any one of the delimitersSplits the calling string each time it finds any one of the delimiters adjacent delimiters split into an empty stringadjacent delimiters split into an empty string The following Example Reads a file in and separates the lines into a string[]: The following Example Reads a file in and separates the lines into a string[]: StreamReader sr = null; try { sr = new StreamReader(“MyFile.txt”); string[] lines = sr.ReadToEnd().Replace(“\r”, “”).Split(‘\n’); } finally { sr.Close(); }
30
C# Strings 30 Tokenizing a string Tokenizing means splitting into parts Tokenizing means splitting into parts Why not use Split() ?Why not use Split() ? Leaves empty strings sometimes Leaves empty strings sometimes We must know all possible tokens (delimiters) We must know all possible tokens (delimiters) (See SearchingStrings Demo)
31
C# Strings 31 Tokenizing using IndexOf() Exactly what we want Exactly what we want No Extra Empty stringsNo Extra Empty strings We only need to know what makes up a word (not what doesn’t)We only need to know what makes up a word (not what doesn’t) Not the most elegant code though... Not the most elegant code though... (See SearchingStrings Demo)
32
C# Strings 32 Introducing Regular Expressions String pattern matching tool String pattern matching tool Regular expressions constitute a language Regular expressions constitute a language C# regular expressions are a language inside a languageC# regular expressions are a language inside a language Used in many languages (Perl most notably) Used in many languages (Perl most notably) There’s a whole class on the theory There’s a whole class on the theory CNS 3240: Computational TheoryCNS 3240: Computational Theory By Chuck AllisonBy Chuck Allison
33
C# Strings 33 Pattern Matching Match any of the characters in brackets [] once Match any of the characters in brackets [] once [a-zA-Z][a-zA-Z] Anything not in brackets is matched exactly Anything not in brackets is matched exactly Except for special charactersExcept for special characters abc[a-zA-Z]abc[a-zA-Z] Match preceding pattern zero or more times Match preceding pattern zero or more times [a-zA-Z]*[a-zA-Z]* Match preceding pattern one or more times Match preceding pattern one or more times [a-zA-Z]+[a-zA-Z]+
34
C# Strings 34 Language Elements ()groups patterns ()groups patterns |“or”, choose between patterns |“or”, choose between patterns []defines a range of characters []defines a range of characters {}used as a quantifier {}used as a quantifier \escape character \escape character. matches any character. matches any character ^beginning of line ^beginning of line $end of line $end of line [^]not character specified [^]not character specified
35
C# Strings 35 Quantifiers *Matches zero or more *Matches zero or more +Matches one or more +Matches one or more ?Matches zero or one ?Matches zero or one {n}Matches exactly n {n}Matches exactly n {n,}Matches at least n {n,}Matches at least n {n,m}Matches at least n, up to m {n,m}Matches at least n, up to m These quantifiers always take the largest pattern they can match These quantifiers always take the largest pattern they can match Lazy quantifiers always take the smallest pattern they can match Lazy quantifiers always take the smallest pattern they can match The lazy quantifiers are the same as those listed above, except followed by a ?The lazy quantifiers are the same as those listed above, except followed by a ?
36
C# Strings 36 Character Classes \wMatches any word character \wMatches any word character Same as: [a-zA-Z_0-9]Same as: [a-zA-Z_0-9] \WMatches any non-word character \WMatches any non-word character Same as: [^a-zA-Z_0-9]Same as: [^a-zA-Z_0-9] \sMatches any white-space character \sMatches any white-space character Same as: [\f\n\r\t\v]Same as: [\f\n\r\t\v] \SMatches any non-white-space character \SMatches any non-white-space character Same as: [^\f\n\r\t\v]Same as: [^\f\n\r\t\v] \dMatches any digit character \dMatches any digit character Same as: [0-9]Same as: [0-9] \DMatches any non-digit character \DMatches any non-digit character Same as: [^0-9]Same as: [^0-9]
37
C# Strings 37 Putting It Together Regular Expression for C# identifiers: Regular Expression for C# identifiers: [a-zA-Z$_][a-zA-Z0-9$_]*[a-zA-Z$_][a-zA-Z0-9$_]* Floating Point Numbers: Floating Point Numbers: (0|([1-9][0-9]*))?\.[0-9]+(0|([1-9][0-9]*))?\.[0-9]+ C# Hexidecimal numbers C# Hexidecimal numbers [0][xX][0-9a-fA-F]+[0][xX][0-9a-fA-F]+ ‘words’ in Project 4 ‘words’ in Project 4 [a-zA-Z\-’]+[a-zA-Z\-’]+
38
C# Strings 38 C# Regular Expressions System.Text.RegularExpressions System.Text.RegularExpressions RegexRegex MatchMatch MatchCollectionMatchCollection CaptureCapture CaptureCollectionCaptureCollection GroupGroup
39
C# Strings 39 Regex Class Exposes static methods for doing Regular Expression matching Exposes static methods for doing Regular Expression matching Or, holds a Regular Expression as an object Or, holds a Regular Expression as an object Compiles the expression to make it fasterCompiles the expression to make it faster
40
C# Strings 40 Regex Members The non-static methods echo the static methods The non-static methods echo the static methods Options Options Escape() Escape() GetGroupNames() GetGroupNames() GetGroupNumbers() GetGroupNumbers() GetNameFromNumber() GetNameFromNumber() GetNumberFromName() GetNumberFromName() IsMatch() IsMatch() Match() Match() Matches() Matches() Replace() Replace() Split() Split() Unescape() Unescape()
41
C# Strings 41 Regex.Options Options Options RegexOptions EnumRegexOptions Enum Compiled – speeds up the searches Compiled – speeds up the searches IgnoreCase IgnoreCase MultiLine MultiLine None None RightToLeft RightToLeft SingleLine SingleLine
42
C# Strings 42 Regex.Escape() If you’re not sure what needs to be escaped? If you’re not sure what needs to be escaped? Regex.Escape(string pattern) Regex.Escape(string pattern) Returns a new string with the necessary characters escapedReturns a new string with the necessary characters escaped Need to undo it? Need to undo it? Regex.Unescape(string pattern) Regex.Unescape(string pattern) Returns a new string with all escape characters removedReturns a new string with all escape characters removed
43
C# Strings 43 Matches private Regex re1 = new Regex(@"(([2-9]\d{2})-)?(\d{3})-(\d{4})"); private string input1 = “801-224-6707"; Match: Value Index Length Success NextMatch() Captures Groups
44
C# Strings 44 Linked Matches Follow links using NextMatch() Follow links using NextMatch() Last link Success == false Last link Success == false Match1 NextMatch() Success==true Match2 NextMatch() Success==true Match3 NextMatch() Success==true Match4 NextMatch() Success=false
45
C# Strings 45 Groups private Regex re1 = new Regex("(([2-9]\d{2})-)?(\d{3})-(\d{4})"); Group: Value Index Length Success Captures 1 2 0 Captures a matching substring for future use Captures a matching substring for future use Captures in a Capture objectCaptures in a Capture object Group 0 represents the entire match Group 0 represents the entire match 34
46
C# Strings 46 Named Groups (? expression) (? expression) Non-capturing group Non-capturing group (?:expression)(?:expression)(@"(?:(?<areaCode>[2-9]\d{2})-)?(?<prefix>\d{3})-(?<lastFour>\d{4})
47
C# Strings 47 Captures Capture: Value Index Length private Regex re1 = new Regex(@"(([2-9]\d{2})-)?(\d{3})-(\d{4}) "); private Regex re1 = new Regex(@"(([2-9]\d{2})-)?(\d{3})-(\d{4}) "); private string input1 = “801-224-6707"; private string input1 = “801-224-6707";
48
C# Strings 48 Regex.Match() Regex.IsMatch(string input, string pattern) Regex.IsMatch(string input, string pattern) returns true if input matches pattern at least oncereturns true if input matches pattern at least once Regex.Match(string input, string pattern) Regex.Match(string input, string pattern) Returns a Match objectReturns a Match object Use Match.Value to get the string value of the matchUse Match.Value to get the string value of the match Regex.Matches(string input, string pattern) Regex.Matches(string input, string pattern) Returns a MatchCollection of all the occurrences of pattern in inputReturns a MatchCollection of all the occurrences of pattern in input
49
C# Strings 49 Regex Groups GetGroupNames() GetGroupNames() Returns all the group names in a string[]Returns all the group names in a string[] GetGroupNumbers() GetGroupNumbers() Returns the group numbers in an int[]Returns the group numbers in an int[] GetNameFromNumber() GetNameFromNumber() GetNumberFromName() GetNumberFromName()
50
C# Strings 50 Regex.Split() Splits the string on a Regular Expression Pattern Splits the string on a Regular Expression Pattern string input = "one%two%%three%%four"; Console.WriteLine(); Console.WriteLine("Split..."); Console.WriteLine(string.Join(",", Regex.Split(input, @"[%]+"))); Console.ReadLine();
51
C# Strings 51 Regex.Replace Refer to a group capture in the regex using a $ Refer to a group capture in the regex using a $ Replace(string input, string replacement, int count) Replace(string input, string replacement, int count) string input2 = "aaabbbccc:aaabbbccc:aaabbbccc"; Regex re2 = new Regex("(aaa)(bbb)(ccc)"); Console.WriteLine();Console.WriteLine("Replace..."); Console.WriteLine(re2.Replace(input2, "$3$2$1", 1)); Console.ReadLine();
52
C# Strings 52 Constructing Strings Becuase strings are immutable, building them is slow Becuase strings are immutable, building them is slow Each change creates a new stringEach change creates a new string Use StringBuilder to speed things up Use StringBuilder to speed things up
53
C# Strings 53 StringBuilder In System.Text In System.Text Contains a mutable string Contains a mutable string Allocates space as needed Allocates space as needed Build the string then call: Build the string then call: myStringBuilder.ToString()myStringBuilder.ToString()
54
C# Strings 54 StringBuilder Members Capacity Capacity Indexer Indexer Length Length Append() Append() AppendFormat() AppendFormat() Insert() Insert() Remove() Remove() Replace() Replace() ToString() ToString()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.