SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Strings in C#
Dr. Neeraj Kumar Pandey
Strings in C#
 In C# System.String class is used to manipulate strings.
 System.String is a very powerful and versatile class, but it is not by
any means the only string-related class in the .NET armory.
 System.Text and System.Text.RegularExpression namespaces are
also used in Building Strings,Formatting Expressions and Regular
Expression
Dr. Neeraj Kumar
Pandey
Immutable Strings
 In this type of strings, we cannot modify the characters of a string.
Dr. Neeraj Kumar
Pandey
Immutable Strings
Dr. Neeraj Kumar
Pandey
Comparing Strings
Compare() Method
int n=string.Compare(s1,s2);
 Zero interger, if s1 is equal to s2
 A positive Integer (1) , if s1 is greater than s2
 A negative integer(-1), if s1 is less than s2
Equals() Method
There are two versions of Equals method. They are implemented as follows:-
bool b1= s2.Equals(s1);
bool b2= string.Equals(s2,s1);
These methods returns a Boolean value true if s1 and s2 are equal, otherwise false.
The == Operator
bool b3= (s1==s2); //b3 is true if they are equal
We very often use such statements in decision statements, like:-
if(s1==s2)
Dr. Neeraj Kumar
Pandey
Mutable Strings
 This type of strings are modifiable using the StringBuilder class.
StringBuilder str1=new StringBuilder(“hello”);
StringBuilder str2=new StringBuilder();
 They can grow dynamically as more characters are added to them.
 They can grow either unbounded or up to a configurable maximum.
 Mutable Strings are also known as dynamic strings.
Dr. Neeraj Kumar
Pandey
Mutable Strings(Contd..)
 The System.Text namespace contains the class StringBuilder,
therefore we must include the using System.Text directive for
creating and manipulating mutable strings.
Dr. Neeraj Kumar
Pandey
Mutable String Program
using System;
using System.Text;
class strbuild
{
public static void Main()
{
string str1,str2;
Console.WriteLine("Enter first string");
str1=Console.ReadLine();
Console.WriteLine("Enter Second string");
str2=Console.ReadLine();
StringBuilder s1 = new StringBuilder(str1);
StringBuilder s2 = new StringBuilder(str2);
//1.Appending a String
s1.Append("Smile");
// Console.WriteLine("String After Append :" +s1);
//2.Append Format
s2.AppendFormat("1)s2 after Append Format {0}",s2);
s1.AppendFormat("2)s1 after Append Format {0}",s1);
//3.Ensure Capacity
int x=s1.EnsureCapacity(30);
//4.Inserting a String
s2.Insert(3,"Life");
//5.Remove
s1.Remove(5,3);
//6.Replace
s2.Replace('A', '*');
//7.Capacity
int y= s1.Capacity;
Console.WriteLine("Capacity is"+y);
s1.Capacity=100;
y=s1.Capacity;
Console.WriteLine("New Capacity is"+y);
//8.Length
int z = s1.Length;
int v = s2.Length;
Console.WriteLine(" Length of s1 "+ z);
Console.WriteLine("Length of s2"+ v);
//9.MaxCapacity
int c= s1.MaxCapacity;
Console.WriteLine("Max Capacity"+s1);
//10.setting a character
int n=s2.Length;
s2[n-1]='@';
}
}
Dr. Neeraj Kumar
Pandey
Regular Expression
 A regular expression may be applied to a text to accomplish tasks
such as :-
 To locate substring and return them
 To modify one or more substrings and return them
 To identify substrings that begins with or end with a pattern of
characters.
 To find all the words that begin with a group of characters and end
with some other characters
 To find all the occurrences of a substring pattern.
 System.Text.RegualrExpressions supports a number of classes that
can be used for searching, matching and modifying a text
document.The important classes are:-
 Regex, MathCollection, Match.
Dr. Neeraj Kumar
Pandey
Regular Expression Program
using System;
using System.Text;
using System.Text.RegularExpressions;
class bulb
{
public static void Main()
{
string str;
str= Console.ReadLine();
Regex reg= new Regex(" |, ");
StringBuilder sb= new StringBuilder();
int count=1;
foreach(string sub in reg.Split(str))
{
sb.AppendFormat("{0}:{1}n",count++,sub);
}
Console.WriteLine(sb);
}
}
Dr. Neeraj Kumar
Pandey

More Related Content

What's hot (20)

PPT
C# basics
Dinesh kumar
 
PPT
Literals,variables,datatype in C#
Prasanna Kumar SM
 
PPT
7.data types in c#
Zeeshan Ahmad
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PDF
Java collections
Hamid Ghorbani
 
PPTX
OOP concepts -in-Python programming language
SmritiSharma901052
 
PPTX
Control Flow Statements
Tarun Sharma
 
PPTX
Concept Of C++ Data Types
k v
 
PPTX
Inheritance in oops
Hirra Sultan
 
PPT
Method overriding
Azaz Maverick
 
PDF
Constructors and Destructors
Dr Sukhpal Singh Gill
 
PPTX
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
PPTX
Member Function in C++
NikitaKaur10
 
PPTX
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
PPTX
Inheritance in java
RahulAnanda1
 
PPTX
Inheritance in Java
Tamanna Akter
 
PPTX
Arrays in Java
Abhilash Nair
 
PPT
Abstract class
Tony Nguyen
 
PPTX
virtual function
VENNILAV6
 
C# basics
Dinesh kumar
 
Literals,variables,datatype in C#
Prasanna Kumar SM
 
7.data types in c#
Zeeshan Ahmad
 
Classes, objects in JAVA
Abhilash Nair
 
Java collections
Hamid Ghorbani
 
OOP concepts -in-Python programming language
SmritiSharma901052
 
Control Flow Statements
Tarun Sharma
 
Concept Of C++ Data Types
k v
 
Inheritance in oops
Hirra Sultan
 
Method overriding
Azaz Maverick
 
Constructors and Destructors
Dr Sukhpal Singh Gill
 
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Member Function in C++
NikitaKaur10
 
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
Inheritance in java
RahulAnanda1
 
Inheritance in Java
Tamanna Akter
 
Arrays in Java
Abhilash Nair
 
Abstract class
Tony Nguyen
 
virtual function
VENNILAV6
 

Similar to Strings in c# (20)

PPT
13 Strings and text processing
maznabili
 
PPTX
16 strings-and-text-processing-120712074956-phpapp02
Abdul Samee
 
PDF
DSA - Lecture 04
Haitham El-Ghareeb
 
PDF
C# chap 10
Shehrevar Davierwala
 
PPT
Strings Arrays
phanleson
 
PPTX
13 Strings and Text Processing
Intro C# Book
 
PDF
LectureNotes-04-DSA
Haitham El-Ghareeb
 
PPSX
String and string manipulation x
Shahjahan Samoon
 
PPTX
C# String
Raghuveer Guthikonda
 
PDF
OOPs difference faqs- 4
Umar Ali
 
PPTX
Java string handling
GaneshKumarKanthiah
 
PPT
String and string manipulation
Shahjahan Samoon
 
PDF
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
PDF
05 c++-strings
Kelly Swanson
 
PDF
Module 6 - String Manipulation.pdf
MegMeg17
 
PPTX
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
 
PPTX
Java string handling
Salman Khan
 
PPTX
String handling
ssuser20c32b
 
PPTX
Java String
SATYAM SHRIVASTAV
 
PPTX
Strings-in-Java-An-Overview tyyyyyyyhjjjjj
omkarpriyadarsan2
 
13 Strings and text processing
maznabili
 
16 strings-and-text-processing-120712074956-phpapp02
Abdul Samee
 
DSA - Lecture 04
Haitham El-Ghareeb
 
Strings Arrays
phanleson
 
13 Strings and Text Processing
Intro C# Book
 
LectureNotes-04-DSA
Haitham El-Ghareeb
 
String and string manipulation x
Shahjahan Samoon
 
OOPs difference faqs- 4
Umar Ali
 
Java string handling
GaneshKumarKanthiah
 
String and string manipulation
Shahjahan Samoon
 
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
05 c++-strings
Kelly Swanson
 
Module 6 - String Manipulation.pdf
MegMeg17
 
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
 
Java string handling
Salman Khan
 
String handling
ssuser20c32b
 
Java String
SATYAM SHRIVASTAV
 
Strings-in-Java-An-Overview tyyyyyyyhjjjjj
omkarpriyadarsan2
 
Ad

More from Dr.Neeraj Kumar Pandey (19)

PPTX
Structure in c#
Dr.Neeraj Kumar Pandey
 
PPTX
Program control statements in c#
Dr.Neeraj Kumar Pandey
 
PPTX
Operators and expression in c#
Dr.Neeraj Kumar Pandey
 
PPTX
Method parameters in c#
Dr.Neeraj Kumar Pandey
 
PPTX
Enumeration in c#
Dr.Neeraj Kumar Pandey
 
PPTX
C# classes objects
Dr.Neeraj Kumar Pandey
 
PPTX
Dot net assembly
Dr.Neeraj Kumar Pandey
 
PPT
Cloud introduction
Dr.Neeraj Kumar Pandey
 
PPTX
Role of cloud computing in scm
Dr.Neeraj Kumar Pandey
 
PPTX
Public cloud
Dr.Neeraj Kumar Pandey
 
PPTX
cloud computing Multi cloud
Dr.Neeraj Kumar Pandey
 
PPTX
Ibm bluemix case study
Dr.Neeraj Kumar Pandey
 
PPTX
Business cases for the need of cloud computing
Dr.Neeraj Kumar Pandey
 
PPT
cloud computing:Types of virtualization
Dr.Neeraj Kumar Pandey
 
PPTX
cloud computing: Vm migration
Dr.Neeraj Kumar Pandey
 
PPTX
Cloud Computing: Virtualization
Dr.Neeraj Kumar Pandey
 
PPT
Dot net introduction
Dr.Neeraj Kumar Pandey
 
PPTX
C# lecture 2: Literals , Variables and Data Types in C#
Dr.Neeraj Kumar Pandey
 
PPTX
C# lecture 1: Introduction to Dot Net Framework
Dr.Neeraj Kumar Pandey
 
Structure in c#
Dr.Neeraj Kumar Pandey
 
Program control statements in c#
Dr.Neeraj Kumar Pandey
 
Operators and expression in c#
Dr.Neeraj Kumar Pandey
 
Method parameters in c#
Dr.Neeraj Kumar Pandey
 
Enumeration in c#
Dr.Neeraj Kumar Pandey
 
C# classes objects
Dr.Neeraj Kumar Pandey
 
Dot net assembly
Dr.Neeraj Kumar Pandey
 
Cloud introduction
Dr.Neeraj Kumar Pandey
 
Role of cloud computing in scm
Dr.Neeraj Kumar Pandey
 
cloud computing Multi cloud
Dr.Neeraj Kumar Pandey
 
Ibm bluemix case study
Dr.Neeraj Kumar Pandey
 
Business cases for the need of cloud computing
Dr.Neeraj Kumar Pandey
 
cloud computing:Types of virtualization
Dr.Neeraj Kumar Pandey
 
cloud computing: Vm migration
Dr.Neeraj Kumar Pandey
 
Cloud Computing: Virtualization
Dr.Neeraj Kumar Pandey
 
Dot net introduction
Dr.Neeraj Kumar Pandey
 
C# lecture 2: Literals , Variables and Data Types in C#
Dr.Neeraj Kumar Pandey
 
C# lecture 1: Introduction to Dot Net Framework
Dr.Neeraj Kumar Pandey
 
Ad

Recently uploaded (20)

PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 

Strings in c#

  • 1. Strings in C# Dr. Neeraj Kumar Pandey
  • 2. Strings in C#  In C# System.String class is used to manipulate strings.  System.String is a very powerful and versatile class, but it is not by any means the only string-related class in the .NET armory.  System.Text and System.Text.RegularExpression namespaces are also used in Building Strings,Formatting Expressions and Regular Expression Dr. Neeraj Kumar Pandey
  • 3. Immutable Strings  In this type of strings, we cannot modify the characters of a string. Dr. Neeraj Kumar Pandey
  • 5. Comparing Strings Compare() Method int n=string.Compare(s1,s2);  Zero interger, if s1 is equal to s2  A positive Integer (1) , if s1 is greater than s2  A negative integer(-1), if s1 is less than s2 Equals() Method There are two versions of Equals method. They are implemented as follows:- bool b1= s2.Equals(s1); bool b2= string.Equals(s2,s1); These methods returns a Boolean value true if s1 and s2 are equal, otherwise false. The == Operator bool b3= (s1==s2); //b3 is true if they are equal We very often use such statements in decision statements, like:- if(s1==s2) Dr. Neeraj Kumar Pandey
  • 6. Mutable Strings  This type of strings are modifiable using the StringBuilder class. StringBuilder str1=new StringBuilder(“hello”); StringBuilder str2=new StringBuilder();  They can grow dynamically as more characters are added to them.  They can grow either unbounded or up to a configurable maximum.  Mutable Strings are also known as dynamic strings. Dr. Neeraj Kumar Pandey
  • 7. Mutable Strings(Contd..)  The System.Text namespace contains the class StringBuilder, therefore we must include the using System.Text directive for creating and manipulating mutable strings. Dr. Neeraj Kumar Pandey
  • 8. Mutable String Program using System; using System.Text; class strbuild { public static void Main() { string str1,str2; Console.WriteLine("Enter first string"); str1=Console.ReadLine(); Console.WriteLine("Enter Second string"); str2=Console.ReadLine(); StringBuilder s1 = new StringBuilder(str1); StringBuilder s2 = new StringBuilder(str2); //1.Appending a String s1.Append("Smile"); // Console.WriteLine("String After Append :" +s1); //2.Append Format s2.AppendFormat("1)s2 after Append Format {0}",s2); s1.AppendFormat("2)s1 after Append Format {0}",s1); //3.Ensure Capacity int x=s1.EnsureCapacity(30); //4.Inserting a String s2.Insert(3,"Life"); //5.Remove s1.Remove(5,3); //6.Replace s2.Replace('A', '*'); //7.Capacity int y= s1.Capacity; Console.WriteLine("Capacity is"+y); s1.Capacity=100; y=s1.Capacity; Console.WriteLine("New Capacity is"+y); //8.Length int z = s1.Length; int v = s2.Length; Console.WriteLine(" Length of s1 "+ z); Console.WriteLine("Length of s2"+ v); //9.MaxCapacity int c= s1.MaxCapacity; Console.WriteLine("Max Capacity"+s1); //10.setting a character int n=s2.Length; s2[n-1]='@'; } } Dr. Neeraj Kumar Pandey
  • 9. Regular Expression  A regular expression may be applied to a text to accomplish tasks such as :-  To locate substring and return them  To modify one or more substrings and return them  To identify substrings that begins with or end with a pattern of characters.  To find all the words that begin with a group of characters and end with some other characters  To find all the occurrences of a substring pattern.  System.Text.RegualrExpressions supports a number of classes that can be used for searching, matching and modifying a text document.The important classes are:-  Regex, MathCollection, Match. Dr. Neeraj Kumar Pandey
  • 10. Regular Expression Program using System; using System.Text; using System.Text.RegularExpressions; class bulb { public static void Main() { string str; str= Console.ReadLine(); Regex reg= new Regex(" |, "); StringBuilder sb= new StringBuilder(); int count=1; foreach(string sub in reg.Split(str)) { sb.AppendFormat("{0}:{1}n",count++,sub); } Console.WriteLine(sb); } } Dr. Neeraj Kumar Pandey