本文介绍了使用C#删除asp.net中的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

在这里我想删除一个子字符串:

例如:天使保育学校; 001

我要删除; 001

但是我写了这样的代码:

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:

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



但是,如果有任何问题,请不要删除; 001,建议我.


问候,

Anilkumar.D



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


Regards,

Anilkumar.D

推荐答案

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



如果每个字符串都包含;那么您可以按如下所示进行拆分



if every string contains ; then you can split as below

string[] str=text.split(';')
//In str[0] you will get school name.



希望这对您有帮助



Hope this helps you


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

str[0]=Angel Nursery School;


class Program
{
    static void Main()
    {
	string input = "Angel Nursery School; 001";
	string sub = input.Substring(6);
	Console.WriteLine("Substring: {0}", sub);
    }
}



它将返回:托儿所; 001

重载2: Substring(int Starteindex, int Endindex)-它将返回startindex和Endindex之间的所有字符
例如



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.

class Program
{
    static void Main()
    {
	string input = "Angel Nursery School; 001";
	string sub = input.Substring(6, 13);
	Console.WriteLine("Substring: {0}", sub);
    }
}



它将返回:托儿所

现在回到您的情况.如果要删除001,则将代码修改为:



It will return : Nursery

Now coming back to your case. If you want to remove 001, then modify your code as :

String text = ddlschoolname.Text;
String numbers = text;
if (text.Length >= 10)
{
    numbers = text.Substring(0, text.Length - 3);
}



希望这会有所帮助.
一切顺利.



Hope this helps.
All the best.


这篇关于使用C#删除asp.net中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 21:18