You are on page 1of 3

Lecture overview

String manipulation
‰ String formatting
in C# ‰ Stringbuilder and string methods
‰ Regular expressions
Lecture 9

CS 638 Web Programming

CS 638 Web Programming – Estan & Kivolowitz

String formatting Some formatting codes


‰ String.Format(formatstring, arguments) ‰ For numbers n – use comas to separate thousands,e
‰ Also supported by Console.WriteLine() and others – scientific notation, x and X – hexadecimal, 0
indicates padding with 0s, # indicates position of digits
‰ Format string contains groups of the form (see demo4)
{index[,alignment][:codes]} ‰ For dates and times: d and D – short/long date, t/T –
‰ Index of first argument after format string is 0 short/long time, mm – a minutes, MM – month as
‰ Alignment specifies number of characters to use (padded number, MMM – month as 3 letter code, MMMM –
with spaces at left if positive, at right if negative) month name
‰ Codes are interpreted based on the value’s type ‰ g stands for the generic default format for all types
‰ Alignment and codes can be omitted ‰ For more details on formatting see
http://blog.stevex.net/index.php/string-formatting-
‰ Relies extensively on objects’ ToString() method
in-csharp/

CS 638 Web Programming – Estan & Kivolowitz CS 638 Web Programming – Estan & Kivolowitz

String manipulation methods StringBuilder


‰ Some methods of the String class ‰ Strings are immutable objects
‰ Trim() removes whitespaces from both ends of string ‰ Whenever a new string created, it uses a new
‰ Split(char[] separators) splits string into an array memory location
of substrings separated by the given characters ‰ This happens whenever strings are concatenated,
‰ SubString(int index, int length) extracts the trimmed, characters replaced, etc.
substring of given length from the given position ‰ Inefficient if a large string built by many small changes
‰ IndexOf(string substring, int startIndex)
finds first occurrence of given substring after given index
‰ The StringBuilder class allows more efficient in-
‰ LastIndexOf(string substring) finds last index place manipulation
where substring occurs ‰ Appending strings, replacing substrings and characters,
‰ Replace(string oldValue, string newValue) removing substrings, etc.

CS 638 Web Programming – Estan & Kivolowitz CS 638 Web Programming – Estan & Kivolowitz

1
Regular expressions Example regular expressions 1
‰ A regular expression (regex) is a compact way of Regex Strings that match Strings that don’t match
representing a certain type of pattern a “abc”,“cba” “xyz”

‰ For most patterns, multiple equivalent regexes exist ^a “abc” “cba”,“xyz”


a$ “cba” “abc”,“xyz”
‰ Fundamental operation: regex matching – deciding ^[a-z]$ “a”,“x” “abc”,“A”,“^[a-z]$”
if a given input string can be mapped to the pattern ^[a-z0-9+-]$ “q”,“+”,“-”,“5” “15”,“Q”
‰ Studied by complexity theory – simple to match ^[^a-zA-Z]$ “5”,“+” “p”,“R”,“abc”,“15”

‰ Many applications, among them ^[a-zA-Z] “A”,“q0”,“abc” “28”,“ f”


[^a-zA-Z] “q0”,“28”,“ f” “A”,“abc”
‰ Used by compilers as a first step of program analysis
^.$ “m”,“3”, “%” “13”,“e4”,“xyz”
‰ Various popular Unix commands such as grep
. “%”,“e4”,“xyz” “”
‰ In web programming mostly for validating user input

CS 638 Web Programming – Estan & Kivolowitz CS 638 Web Programming – Estan & Kivolowitz

Example regular expressions 2 Example regular expressions 3


Regex Strings that match Strings that don’t match Regex Strings that match Strings that don’t match
abc “abcde”,“rabco” “a”,“ABC”,“bcde” ^a*$ “a”,“aaaa”,“” “bart”
^abc “abc”,“abcde” “rabco”,“ABC”,“bcde” ^a+$ “a”,“aaaa” “bart”,“”
E[0-9] “ME3”,“E85”,“EBE5” “ACME”,“E”,“E 8” ^a?$ “a”,“” “aaaa”,“aa”
E[0-9]$ “ACME3”,“EBE5” “E85”,“E3EB”,“E 8” ^a{4}$ “aaaa” “a”,“”,“aa”
foo|bar “foo”,”bart” “ooba” ^a{2,6}$ “aaaa”,“aa” “a”,“”,“aaaaaaaaaa”
a(b|c)d “Tabd”,“acdc” “abcd” (e|o){2} “meow”,“booooom” “kenmore”
^foo|bar “fool”,“Anbar” “snafoo” e|(o{2}) “nest”, “boooom” “port”
^(foo|bar) “fool” “Anbar”,“snafoo” (a[0-9]*){2} “aa”,“ba45a” “abacus”,“a3b8a0”

CS 638 Web Programming – Estan & Kivolowitz CS 638 Web Programming – Estan & Kivolowitz

Regular expressions in C#
‰ Implemented by the class
System.Text.RegularExpressions.Regex C# programming
‰ Constructor accepts a string describing the regular
expression that is “compiled” to a representation
used for efficient matching Lectures 6 - 9
‰ Important methods
‰ IsMatch(string input) checks if input string matches
‰ Replace(string input, string replacement)
replaces all matches of the regular expression in input CS 638 Web Programming
‰ Split(string input) splits input interpreting each
match of the regex as a separator
‰ See demo4 for examples on how to use regexes

CS 638 Web Programming – Estan & Kivolowitz

2
Differences between
C# and Java Application structure
‰ Application structure ‰ Namespaces similar to Java packages, but
decoupled from how source code is structured
‰ Inheritance and polymorphism
‰ Multiple classes can be defined in a single file, name
‰ Value types and parameter passing of classes unrelated to file name
‰ Syntax changes and extensions ‰ C# partial classes – a class defined in multiple files
‰ For more detailed comparison that goes ‰ Assemblies similar to jar files
beyond the things we covered in class see ‰ C#’s keyword internal is like Java’s protected
http://www.25hoursaday.com/CsharpVsJava.html – grants access to others from same assembly
‰ In C# protected grants access to derived classes

CS 638 Web Programming – Estan & Kivolowitz CS 638 Web Programming – Estan & Kivolowitz

Inheritance and polymorphism Value types


‰ In C# you must explicitly use the virtual keyword for ‰ C# has structs which are value types
methods overridden in derived classes (in Java ‰ new is optional (no memory allocation, calls constructor)
methods are virtual by default) ‰ Generics in C# can also use value types (not just
‰ By default C# methods are like final methods in Java classes as in Java)
‰ Derived class can still specify new method, but it does not ‰ Parameter passing
lead to polymorphic behavior
‰ Java passes all parameters by value
‰ Operator overloading supported in C#, not in Java ‰ What does it mean to pass a reference type by value?

‰ C# replaces the implements and extends ‰ C# allows passing by reference and output parameters for
keywords with : both value and reference types
‰ C# refers to the base class as base, not super ‰ Boolean variables are of type bool, not boolean

CS 638 Web Programming – Estan & Kivolowitz CS 638 Web Programming – Estan & Kivolowitz

Syntax changes and


extensions Concepts
‰ The foreach loop has different syntax ‰ Event-driven programming
‰ In C# foreach(int i in numbers) ‰ Extending applications with new event handlers
‰ In Java for(int i:numbers)
‰ Debugging – breakpoints, stepping through
‰ Changes to switch statement, program, watches, assertions
‰ Can use string literals for case clauses
‰ Using language features that make it easier
‰ Fall-through between cases is forbidden
for the compiler to catch mistakes
‰ C# objects can have properties (use of accessors) ‰ Enums, const, readonly
‰ C# has delegates
‰ Operator overloading can help or harm
‰ C# has keywords const and readonly
‰ Naming conventions (for interface names)

CS 638 Web Programming – Estan & Kivolowitz CS 638 Web Programming – Estan & Kivolowitz

You might also like