Dr SNS RAJALAKSHMI COLLEGE OF ARTS AND SCIENCE

  • Slides: 15
Download presentation
Dr. SNS RAJALAKSHMI COLLEGE OF ARTS AND SCIENCE (AUTONOMOUS) COIMBATORE-641049 Accredited by NAAC(Cycle III)

Dr. SNS RAJALAKSHMI COLLEGE OF ARTS AND SCIENCE (AUTONOMOUS) COIMBATORE-641049 Accredited by NAAC(Cycle III) with “A+” Grade Recognised by UGC, Approved by AICTE, New Delhi and Affiliated to Bharathiar University, Coimbatore. DEPARTMENT OF COMPUTER SCIENCE 16 PCS 107: C# Dot Net Programming II YEAR - III SEM UNIT 3 – OPERATORS & EXPRESSION TOPIC – STRING 9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

Outline üString üstring vs String üString Example üC# String methods 9/25/2020 SNS DESIGN THINKERS/

Outline üString üstring vs String üString Example üC# String methods 9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

String In C#, string is an object of System. String class that represent sequence

String In C#, string is an object of System. String class that represent sequence of characters. We can perform many operations on strings such as concatenation, comparision, getting substring, search, trim, replacement etc. 9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

String vs String In C#, string is keyword which is an alias for System.

String vs String In C#, string is keyword which is an alias for System. String class. That is why string and String are equivalent. We are free to use any naming convention. string s 1 = "hello"; //creating string using string keyword String s 2 = "welcome"; //creating string using String class 9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

String Example using System; public class String. Example { public static void Main(string[] args)

String Example using System; public class String. Example { public static void Main(string[] args) { string s 1 = "hello"; char[] ch = { 'c', 's', 'h', 'a', 'r', 'p' }; string s 2 = new string(ch); Console. Write. Line(s 1); Console. Write. Line(s 2); } } OUTPUT: 9/25/2020 hello csharp SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

String Methods Method Name Description Clone() It is used to return a reference to

String Methods Method Name Description Clone() It is used to return a reference to this instance of String. Compare(String, String) It is used to compares two specified String objects. It returns an integer that indicates their relative position in the sort order. Compare. Ordinal(String, String) It is used to compare two specified String objects by evaluating the numeric values of the corresponding Char objects in each string. . Compare. To(String) It is used to compare this instance with a specified String object. It indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified string. Concat(String, String) It is used to concatenate two specified instances of String. 9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

Contd… Methods Description Copy(string) It is used to create a new instance of String

Contd… Methods Description Copy(string) It is used to create a new instance of String with the same value as a specified String. Equals(string, string) It is used to determine that two specified String objects have the same value. Replace(string, String) It is used to return a new string in which all occurrences of a specified string in the current instance are replaced with another specified string. Substring(Int 32) It is used to retrieve a substring from this instance. The substring starts at a specified character position and continues to the end of the string. To. Lower() It is used to convert String into lowercase. To. Upper() It is used to convert String into Uppercase. 9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

Important String Methods. C# String Compare() The C# Compare() method is used to compare

Important String Methods. C# String Compare() The C# Compare() method is used to compare first string with second string lexicographically. It returns an integer value. If both strings are equal, it returns 0. If first string is greater than second string, it returns 1 else it returns -1. Rule s 1==s 2 returns 0 s 1>s 2 returns 1 s 1<s 2 returns -1 9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

Compare() Example using System; public class String. Example { public static void Main(string[] args)

Compare() Example using System; public class String. Example { public static void Main(string[] args) { string s 1 = "hello"; string s 2 = "hello"; string s 3 = "csharp"; string s 4 = "mello"; Console. Write. Line(string. Compare(s 1, s 2)); Console. Write. Line(string. Compare(s 2, s 3)); Console. Write. Line(string. Compare(s 3, s 4)); } } Output: 0 9/25/2020 1 -1 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

String length A string in C# is actually an object, which contain properties and

String length A string in C# is actually an object, which contain properties and methods that can perform certain operations on strings. For example, the length of a string can be found with the Length property: . Example: using System; namespace My. Application { class Program { static void Main(string[] args) { string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Console. Write. Line("The length of the txt string is: " + txt. Length); } } } OUTPUT: The length of the txt string is: 26 9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

String copy The C# Copy() method is used to create a new instance of

String copy The C# Copy() method is used to create a new instance of String with the same value as a specified String. It is a static method of String class. Its return type is string. . EXAMPLE using System; public class String. Example { public static void Main(string[] args) { string s 1 = "Hello "; string s 2 = string. Copy(s 1); Console. Write. Line(s 2); } } OUTPUT: Hello 9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

String Concat() To join one string with other string-use string concat() method. Example: using

String Concat() To join one string with other string-use string concat() method. Example: using System; namespace My. Application { class Program { static void Main(string[] args) { string first. Name = “Rock "; string last. Name = “Stars"; String name = string. Concat(first. Name, last. Name); Console. Write. Line(name); }}} Output: Rock Stars 9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

String Replace() The C# Replace() method is used to get a new string in

String Replace() The C# Replace() method is used to get a new string in which all occurrences of a specified Unicode character in this string are. replaced with another specified Unicode character. Example: using System; public class String. Example { public static void Main(string[] args) { string s 1 = "Hello F#"; string s 2 = s 1. Replace('F', 'C'); Console. Write. Line(s 2); } } OUTPUT: Hello C# 9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT

9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA

9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT

9/25/2020 SNS DESIGN THINKERS/ Dr. SNSRCAS / CS / 16 PCS 107: C# DOT NET PROGRAMMING /UNIT-3/ Ms R. SARANYA