0% found this document useful (0 votes)
6 views5 pages

C# String Manipulation Examples

The document demonstrates various string manipulation techniques in C# using the StringBuilder and string classes. It includes examples of string append, replace, copy, split, and substring. The examples append text to a StringBuilder, replace parts of a string, copy and modify a string, split a string into an array using a delimiter, and get a substring from an original string. Each example shows the relevant code, inputs, and outputs to demonstrate how each string method works.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

C# String Manipulation Examples

The document demonstrates various string manipulation techniques in C# using the StringBuilder and string classes. It includes examples of string append, replace, copy, split, and substring. The examples append text to a StringBuilder, replace parts of a string, copy and modify a string, split a string into an array using a delimiter, and get a substring from an original string. Each example shows the relevant code, inputs, and outputs to demonstrate how each string method works.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Aim:- Demonstrate some basic string manipulation using both the String Builder

and String Classes

Source code:-
1. string append

using System;
using [Link];

namespace BuilderAppend
{
class Appender
{
static void Main(string[] args)
{
[Link]("Please Enter Your Name:");
string Name = [Link]();
StringBuilder Greeting = new StringBuilder();

[Link]("Good Morning");

if ([Link] > 0)
{
[Link](", "+Name+"!");
}
else
{
[Link]("!");
}

[Link](Greeting);
[Link]();

}
}
}

Output:-
please Enter Your Name:

This is cec college

Good Morning, This is cec college


2. replace string
using System;
using [Link];

namespace BuilderReplace
{
class Replacer
{
static void Main(string[] args)
{
[Link]("Please Enter Your Name:");
string Name = [Link]();
StringBuilder Greeting = new StringBuilder();

[Link]("Good Morning!");

if ([Link] > 0)
{
[Link](12,", "+ Name);
}

[Link](Greeting);

[Link]("Now Enter a Nickname:");


string NickName = [Link]();

if([Link] > 0 && [Link] > 0)


{
[Link](Name, NickName);
}

[Link](Greeting);
[Link]();

}
}
}

Output:-
Please Enter Your Name :
Ceccollege
Good Morning , Ceccollege
Now Enter nick name
College
Good Morning, College
3 string Copy
using System;

namespace StringCopy
{
class StringCopier
{
[STAThread]
static void Main(string[] args)
{
[Link]("An Example of String Copy");
[Link]("Type Some Text");
string inputText = [Link]();
string copiedText = [Link](inputText);

copiedText+=" more text";


[Link]("Your text is here: "+inputText);
[Link]("Copy of your text with some
additions: "+copiedText);
[Link]();
}
}
}

Output:-
An Example Of String Copy
Type Some Text
Cec college

Your text hear:ceccollege


Copy of your text with some addition :ceccollege more text
[Link] split

using System;
namespace StringSplit
{
class StringSplitter
{
static void Main(string[] args)
{
string textOriginal = "Value 1, Value 2, Value 3";

string[] textArray = [Link](',');

[Link]("An example of splitting a


String:\r\n\r\n");
[Link]("The string to split:
"+textOriginal);
[Link]("The character to split from: ','");
[Link]("\r\nResults:");
foreach(string newText in textArray)
{
[Link]([Link]().ToString());
}

[Link]("");
[Link]("\r\n\r\nNow rejoin the textarray
with a different delimiter:");
//now rejoin the array of strings
string newJoin = [Link](":", textArray);
[Link](newJoin);
[Link]();

}
}
}

Output:-
An example of splitting a string:
The string to split: value1,value2 value3
Result :
Value1
Value2
Value3
Now rejoin the textarray with a different delimiter:
Value1 :value2:value3
5. string substring

using System;

namespace StringSubstring
{
class Substring
{
static void Main(string[] args)
{

string originalString = "abcdefghijklmnop";


[Link]("A Substring example:");
[Link]("String to Be Searched: " +
originalString);
string returnedString = [Link](4,
3).ToString();
[Link]("Start Index: " + "4");
[Link]("Length: " + "3");
[Link]("Substring: " + returnedString);
[Link]();
}
}
}

Output:-
A Substring example:
String to be Searched :abcdefghijklmnop
Start Index:4
Length :3
Substring:efg

Result:- The programme is executed successfully without any errors

You might also like