Anda di halaman 1dari 5

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

and String Classes

Source code:-
1. string append

using System;
using System.Text;

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

Greeting.Append("Good Morning");

if (Name.Length > 0)
{
Greeting.Append(", "+Name+"!");
}
else
{
Greeting.Append("!");
}

Console.WriteLine(Greeting);
Console.Read();

}
}
}

Output:-
please Enter Your Name:

This is cec college

Good Morning, This is cec college


2. replace string
using System;
using System.Text;

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

Greeting.Append("Good Morning!");

if (Name.Length > 0)
{
Greeting.Insert(12,", "+ Name);
}

Console.WriteLine(Greeting);

Console.WriteLine("Now Enter a Nickname:");


string NickName = Console.ReadLine();

if(Name.Length > 0 && NickName.Length > 0)


{
Greeting.Replace(Name, NickName);
}

Console.WriteLine(Greeting);
Console.Read();

}
}
}

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)
{
Console.WriteLine("An Example of String Copy");
Console.WriteLine("Type Some Text");
string inputText = Console.ReadLine();
string copiedText = string.Copy(inputText);

copiedText+=" more text";


Console.WriteLine("Your text is here: "+inputText);
Console.WriteLine("Copy of your text with some
additions: "+copiedText);
Console.Read();
}
}
}

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
4.string split

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

string[] textArray = textOriginal.Split(',');

Console.WriteLine("An example of splitting a


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

Console.WriteLine("");
Console.WriteLine("\r\n\r\nNow rejoin the textarray
with a different delimiter:");
//now rejoin the array of strings
string newJoin = string.Join(":", textArray);
Console.WriteLine(newJoin);
Console.Read();

}
}
}

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";


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

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