Course String manipulation in C#

Course String manipulation in C#






Course String manipulation in C#

















String formatting


  •  String.Format(formatstring, arguments)
  •  Also supported by Console.WriteLine() and others
  •  Format string contains groups of the form {index[,alignment][:codes]}


  •    Index of first argument after format string is 0
  •    Alignment specifies number of characters to use (padded with spaces at left if positive, at right if negative)
  •    Codes are interpreted based on the value’s type
  •    Alignment and codes can be omitted
  •  Relies extensively on objects’ ToString() method



Some formatting codes

  •  For numbers n – use comas to separate thousands,e – scientific notation, x and X – hexadecimal, 0indicates padding with 0s, # indicates position of digits (see demo4)
  •  For dates and times: d and D – short/long date, t/T – short/long time, mm – a minutes, MM – month asnumber, MMM – month as 3 letter code, MMMM – month name
  •  g stands for the generic default format for all types
  •  For more details on formatting see http://blog.stevex.net/index.php/string-formattingin- csharp/



String manipulation methods

  •  Some methods of the String class
  •  Trim() removes whitespaces from both ends of string
  •  Split(char[] separators) splits string into an array of substrings separated by the given characters
  •  SubString(int index, int length) extracts the substring of given length from the given position
  •  IndexOf(string substring, int startIndex) finds first occurrence of given substring after given index
  •  LastIndexOf(string substring) finds last indexwhere substring occurs
  •  Replace(string oldValue, string newValue)



StringBuilder

  •  Strings are immutable objects
  •  Whenever a new string created, it uses a new memory location
  •  This happens whenever strings are concatenated, trimmed, characters replaced, etc.
  •  Inefficient if a large string built by many small changes
  •  The StringBuilder class allows more efficient inplace manipulation
  •  Appending strings, replacing substrings and characters, removing substrings, etc............













Download Course String manipulation in C#















Course String manipulation in C#





0 commentaires: