使用C#删除asp.net中的子字符串 [英] Removing Substring in asp.net with c#

查看:75
本文介绍了使用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

推荐答案

您可以通过两种方式进行操作.

使用子字符串

要使用此功能,您需要知道有多少个数字
在这里为您的示例,您可以使用此
在这里您需要删除最后4个字符,这就是为什么我给定length-4
代替4,您需要根据需要替换数字.

如果之间有空间;和001然后使用length-5

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

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



希望这对您有帮助



Hope this helps you


我认为最好的解决方案是使用string .split()方法并选择第一个数组

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

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

str[0]=Angel Nursery School;


您错误地使用了Substring方法.我们开始:

子字符串有两个重载:
重载1: Substring(int Starteindex)-将获取起始索引并返回起始索引之后的所有字符
例如
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.
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中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆