How to remove characters from a string in ASP.net 2.0

Hi I have a string like ME_NAME that I am showing as headr in gridview, but now I need to remove the first charaters ME_ from the string and only display NAME in the gridview header. But here I am getting the header values ​​from the database.

0


a source to share


5 answers


I think I have an idea what you are talking about, Joy.

You have a GridView that you directly bind using a data source with the AutoGenerateColumns attribute true. To get the title how you want it to appear, you can modify your SQL query like this:

SELECT
    ME_NAME AS [Name]
FROM
    TABLE

      



EDIT: And as you said you want to do it from the frontend, you can hook into the GridView's OnRowDataBound event and in that write the following code -

if(e.Row.RowType == DataControlRowType.Header)
{
    //Apply the logic for removing the "ME_" string from the header columns.
}

      

+3


a source


Sorry if this is too literal ...



var olsstr = "ME_NAME";
var newstr = olsstr.Remove(0, 3);

      

0


a source


If your strings always start with the same value, or with very limited variations, a simple one .Replace()

should do the job:

String str = "ME_NAME";
String str2 = "ANOTHER_NAME";

str = str.Replace("ME_", String.Empty); // str == "NAME"

str2 = str2.Replace("ME_", String.Empty).Replace("ANOTHER_", String.Empty);

      

If the option is larger, you may need to use Regular Expressions .

0


a source


There are several ways to remove the "ME_" part from a string. Here are a couple you can use:

    string s1 = "ME_NAME";

    string s2 = s1.Replace("ME_", "");
    // or
    s2 = s1.Split('_')[1];
    // or
    s2 = s1.Substring(s1.IndexOf('_') + 1);

      

0


a source


In the second comment, however, it looks like you are taking the wrong approach. If these are the column names from your database, you should really rename them to an SQL statement:

SELECT ME_NAME As Name, ME_BIRTHDAY As Birthday FROM tbl_ME;

      

0


a source







All Articles