Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear Friends,

Here i want to Remove a sub string:

Example:Angel Nursery School; 001

i want to Remove ;001

But i have written a code like this:

C#
String text = ddlschoolname.Text;
String numbers = text;
if (text.Length >= 10)
{
    numbers = text.Substring(text.Length - 6);
}


But the ;001 is not Removing if any thing wrong please suggest me.


Regards,

Anilkumar.D
Posted
Updated 29-Sep-11 18:25pm
v2

I think the best solution is use string .split() method and select the first array

C#
string[] str=text.split(';')

str[0]=Angel Nursery School;
 
Share this answer
 
You are using Substring method wrongly. Here we go:

Substring has two overloads :
Overload 1: Substring(int Starteindex) - It will take the start index and will return all the characters after the start index
e.g.
C#
class Program
{
    static void Main()
    {
	string input = "Angel Nursery School; 001";
	string sub = input.Substring(6);
	Console.WriteLine("Substring: {0}", sub);
    }
}


It will return : Nursery School; 001

Overload 2: Substring(int Starteindex, int Endindex) - It will return all the characters between startindex and Endindex
e.g.
C#
class Program
{
    static void Main()
    {
	string input = "Angel Nursery School; 001";
	string sub = input.Substring(6, 13);
	Console.WriteLine("Substring: {0}", sub);
    }
}


It will return : Nursery

Now coming back to your case. If you want to remove 001, then modify your code as :
C#
String text = ddlschoolname.Text;
String numbers = text;
if (text.Length >= 10)
{
    numbers = text.Substring(0, text.Length - 3);
}


Hope this helps.
All the best.
 
Share this answer
 
v2
Comments
Anil Honey 206 30-Sep-11 0:57am    
Thank Your Very Much.Its Working
Pravin Patil, Mumbai 30-Sep-11 1:39am    
Cool...
You can do it in 2 ways.

using substring

to use this you need to know how many digits are there
here for your example you can use this
here you need to remove last 4 characters thats why i gave length-4
in place of 4 you need to replace the number according to your requirement.

If space is there between ; and 001 then use length-5

text.substring(0,text.length-4);


if every string contains ; then you can split as below
C#
string[] str=text.split(';')
//In str[0] you will get school name.


Hope this helps you
 
Share this answer
 
v3
Comments
Anil Honey 206 30-Sep-11 0:57am    
thanks a lot............

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900