Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
there is a database table it has lesson codes some people insert wrong lessoncodes

same lessons has different alots of codes MATH103 MATH-103 MATH 103 MATH_103 These are same lesson

I will get this lessons from database to gridview like one lesson(I will use distinct) but I will not remove from database this control must be c# code behind

how to remove these characters

I dont know these lesson codes has which characters ? - / _ " " etc.
Posted
Comments
[no name] 12-Mar-13 9:12am    
Not able to understand question correctly. Do you have problem of duplicate code or with invalid (restricted) symbols.
joshrduncan2012 12-Mar-13 9:16am    
If I understand this correctly, I think the OP needs to know how to parse strings and look for individual characters to replace.
Nandakishore G N 12-Mar-13 9:18am    
use regex, match it and remove that characters.

Quite easy to do if you use LINQ.

If you have loaded all the records in a collection, you can simply run Distinct on it.
For e.g. IEnumerable<string> code = codesDistinct();</string>

Here[^] is an example.
Another example[^].
 
Share this answer
 
Comments
Member-2338430 12-Mar-13 9:23am    
I will take data from mysql database it has a lesson code column I will take database to gridview but these column has diffrent lesson codes but same lessons MATH 103 MATH-103 I will take one lesson like MAHT any character I dont know which character remove 103
from database to gridview
Member-2338430 12-Mar-13 9:25am    
so MATH 103 MATH-103 MATH_103 MATH?103 MATH--103 WİLL BE MATH103 but I must do in c# code behind
The following uses Regex to construct the lesson code from various formats. I've also expanded the solution to cater for other lessons e.g. HIST for History, ENGL for English and also to cater for non-upper case entries.
I've used a string array for my checking
C#
private void button2_Click(object sender, EventArgs e)
{
    // various possible entries on the database...
    string[] lessons = {"....", "hist=101", "MATH103","MATH-103", "MATH 103", "MATH_103", "MATH--101", "engl   100"};

    foreach (string singleLesson in lessons)
    {
        // m will contain the lesson number 103, 101, 100 etc
        Match m = Regex.Match(singleLesson, @"\d+", RegexOptions.IgnoreCase);
        // n will contain the lesson type MATH, ENGL, HIST etc etc
        Match n = Regex.Match(singleLesson, @"\b[A-Z|a-z]*", RegexOptions.IgnoreCase);

        // if we have both a lesson type and a lesson number ...
        if (m.Success && n.Success )
        {
            string lesson = n.ToString().ToUpper() + m.ToString();
            Debug.Print(lesson);
        }
        else
            Debug.Print("Invalid Entry");


}


Output:
Invalid Entry<br />
HIST101<br />
MATH103<br />
MATH103<br />
MATH103<br />
MATH103<br />
MATH101<br />
ENGL100<br />
 
Share this answer
 
v2

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