C# - Strings.

Slides:



Advertisements
Similar presentations
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Advertisements

Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Java Programming Strings Chapter 7.
CSM-Java Programming-I Spring,2005 String Handling Lesson - 6.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Fundamental Programming Structures in Java: Strings.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
String StringBuffer. class StringExample { public static void main (String[] args) { String str1 = "Seize the day"; String str2 = new String(); String.
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
28-Jun-15 String and StringBuilder Part I: String.
1 Strings and String Operations  What is a Strings?  Internal Representation of Strings  Getting Substrings from a String  Concatenating Strings 
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
Strings.
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
String Class. Objectives and Goals Identify 2 types of Strings: Literal & Symbolic. Learn about String constructors and commonly used methods Learn several.
Chapter : STRING. Slide 2 9:06:37 AM 1. Introduction String and character processing capabilities –Text editors –Word processors… Expand from previous.
1 Text processing. 2 text processing: Examining, editing, formatting text.  Text processing often involves for loops that examine the characters of a.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 5/27/20161.
Chapter 7: Characters, Strings, and the StringBuilder.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
String Handling StringBuffer class character class StringTokenizer class.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
Strings And other things. Strings Overview The String Class and its methods The char data type and Character class StringBuilder, StringTokenizer classes.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
CSI 3125, Preliminaries, page 1 String. CSI 3125, Preliminaries, page 2 String Class Java provides the String class to create and manipulate strings.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
17-Feb-16 String and StringBuilder Part I: String.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Data Types References:  Data Type:  In computer science and computer programming, a data type or simply type is a.
Chapter 8: Loops, Arrays, Strings Loop statements –do –while –for Arrays –declaration, allocation, initialization, access –multi-dimensional –heterogeneous.
Lecture Set 6 The String and DateTime Data Types Part B – Characters and Strings String Properties and Methods.
Chapter 8 String Manipulation
The String class is defined in the java.lang package
String and StringBuffer classes
Objective 7.04 Apply Built-in String Functions (3%)
Strings, Characters and Regular Expressions
EKT 472: Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Programming in Java Text Books :
String and String Buffers
String String Builder.
String Handling in JAVA
Primitive Types Vs. Reference Types, Strings, Enumerations
String Objects & its Methods
Modern Programming Tools And Techniques-I Lecture 11: String Handling
String and StringBuilder
Chapter 7: Strings and Characters
Object Oriented Programming
MSIS 655 Advanced Business Applications Programming
תרגול מס' 3 עבודה עם מחרוזות (Strings) מתודות (Methods) העברת פרמטרים
Introduction to Java Programming
String and StringBuilder
String and StringBuilder
Java – String Handling.
Compiler Design First Lecture.
Lecture 10 Strings CSE /26/2018.
Lecture 07 String Jaeki Song.
Microsoft Visual Basic 2005: Reloaded Second Edition
String and StringBuilder
String methods 26-Apr-19.
String Methods.
Strings in Java.
Dr. Sampath Jayarathna Cal Poly Pomona
In Java, strings are objects that belong to class java.lang.String .
Presentation transcript:

C# - Strings

strings are array of characters The string keyword is an alias for the System.String class. Creating a String Object By assigning a string literal to a String variable , By using a String class constructor, By using the string concatenation operator (+), By retrieving a property or calling a method that returns a string, By calling a formatting method to convert a value or an object to its string representation.

Example using System; namespace StringApplication { class Program { static void Main(string[] args) string fname, lname; fname = “Anjali "; lname = “Saraswathy"; string fullname = fname + lname; Console.WriteLine("Full Name: {0}", fullname);

//by using string constructor char[] letters = { 'H', 'e', 'l', 'l','o' }; string greetings = new string(letters); Console.WriteLine("Greetings: {0}", greetings); //methods returning string string[] sarray = { "Hello", "From", “Anjali", “S" }; string message = String.Join(" ", sarray); Console.WriteLine("Message: {0}", message);

//formatting method to convert a value DateTime waiting = new DateTime(2016, 8, 10, 13, 58, 1); string chat = String.Format("Message sent at {0:t} on {0:D}", waiting); Console.WriteLine("Message: {0}", chat); }

Properties of the String Class Chars : Gets the Char object at a specified position in the current String object. Length: Gets the number of characters in the current String object.

Methods of the String Class public static int Compare(string strA, string strB) Compares two specified string objects and returns an integer that indicates their relative position in the sort order. public static int Compare(string strA, string strB, bool Case) : Compares two specified string objects and returns an integer that indicates their relative position in the sort order. However, it ignores case if the Boolean parameter is true.

public static string Concat(string str0, string str1): Concatenates two string objects. public static string Concat(string str0, string str1, string str2): Concatenates three string objects. public static string Concat(string str0, string str1, string str2, string str3) : Concatenates four string objects. public bool Contains(string value) : Returns a value indicating whether the specified String object occurs within this string. public static string Copy(string str) : Creates a new String object with the same value as the specified string.

public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)Copies a specified number of characters from a specified position of the String object to a specified position in an array of Unicode characters. public bool EndsWith(string value)Determines whether the end of the string object matches the specified string. public bool Equals(string value)Determines whether the current String object and the specified String object have the same value. public static bool Equals(string a, string b)Determines whether two specified String objects have the same value.

public static string Format(string format, Object arg0)Replaces one or more format items in a specified string with the string representation of a specified object. public int IndexOf(char value)Returns the zero-based index of the first occurrence of the specified Unicode character in the current string. public int IndexOf(string value)Returns the zero-based index of the first occurrence of the specified string in this instance. public int IndexOf(char value, int startIndex)Returns the zero-based index of the first occurrence of the specified Unicode character in this string, starting search at the specified character position.

public int IndexOf(string value, int startIndex)Returns the zero-based index of the first occurrence of the specified string in this instance, starting search at the specified character position. public int IndexOfAny(char[] anyOf)Returns the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. public int IndexOfAny(char[] anyOf, int startIndex)Returns the zero- based index of the first occurrence in this instance of any character in a specified array of Unicode characters, starting search at the specified character position.

public string Insert(int startIndex, string value)Returns a new string in which a specified string is inserted at a specified index position in the current string object. public static bool IsNullOrEmpty(string value)Indicates whether the specified string is null or an Empty string. public static string Join(string separator, params string[] value)Concatenates all the elements of a string array, using the specified separator between each element. public static string Join(string separator, string[] value, int startIndex, int count)Concatenates the specified elements of a string array, using the specified separator between each element.

public int LastIndexOf(char value)Returns the zero-based index position of the last occurrence of the specified Unicode character within the current string object. public int LastIndexOf(string value)Returns the zero-based index position of the last occurrence of a specified string within the current string object. public string Remove(int startIndex)Removes all the characters in the current instance, beginning at a specified position and continuing through the last position, and returns the string.

public string Remove(int startIndex, int count)Removes the specified number of characters in the current string beginning at a specified position and returns the string. public string Replace(char oldChar, char newChar)Replaces all occurrences of a specified Unicode character in the current string object with the specified Unicode character and returns the new string. public string Replace(string oldValue, string newValue)Replaces all occurrences of a specified string in the current string object with the specified string and returns the new string.

public string[] Split(params char[] separator)Returns a string array that contains the substrings in the current string object, delimited by elements of a specified Unicode character array. public string[] Split(char[] separator, int count)Returns a string array that contains the substrings in the current string object, delimited by elements of a specified Unicode character array. The int parameter specifies the maximum number of substrings to return. public bool StartsWith(string value)Determines whether the beginning of this string instance matches the specified string.

public char[] ToCharArray()Returns a Unicode character array with all the characters in the current string object. public char[] ToCharArray(int startIndex, int length)Returns a Unicode character array with all the characters in the current string object, starting from the specified index and up to the specified length. public string ToLower()Returns a copy of this string converted to lowercase.

public string ToUpper()Returns a copy of this string converted to uppercase. public string Trim()Removes all leading and trailing white-space characters from the current String object.

Examples Comparing Strings: using System; namespace StringApplication { class StringProg static void Main(string[] args) string str1 = "This is test";

string str2 = "This is text"; if (String.Compare(str1, str2) == 0) { Console.WriteLine(str1 + " and " + str2 + " are equal."); } else Console.WriteLine(str1 + " and " + str2 + " not equal."); Console.ReadKey() ; } } }

String Contains String: using System; namespace StringApplication { class StringProg static void Main(string[] args) { string str = "This is test"; if (str.Contains("test")) Console.WriteLine("The sequence 'test' was found."); } Console.ReadKey() ; } } }

Getting a Substring: using System; namespace StringApplication { class StringProg static void Main(string[] args) { string str = "Last night I dreamt of San Pedro"; Console.WriteLine(str); string substr = str.Substring(23); Console.WriteLine(substr); }

Joining Strings: using System; namespace StringApplication { class StringProg { static void Main(string[] args) { string[] starray = new string[]{"Down the way nights are dark", "And the sun shines daily on the mountain top“}; string str = String.Join("\n", starray); Console.WriteLine(str); } } }