1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

c# 2 lines with same first charcters how can i delete second one??

Discussion in 'C, C++, C#' started by sandrine10, Apr 18, 2015.

  1. sandrine10

    sandrine10 Power Member

    Joined:
    Apr 14, 2010
    Messages:
    638
    Likes Received:
    65
    Gender:
    Female
    Location:
    FreeLand
    Hey Guys,

    as mentionned in the title,in c# if 2 lines have same first characters how can i delete second one?!
    lines like this:

    url1/page1/title1/number1
    url1/page1/title1/number2

    wanna keep the first link and delete the second one,any idea?
     
    Last edited: Apr 18, 2015
  2. qrazy

    qrazy Senior Member

    Joined:
    Mar 19, 2012
    Messages:
    1,115
    Likes Received:
    1,723
    Location:
    Banana Republic
    You might need to use Regex pattern to find the numbers after last trailing slash. Post your code snippet, you might get better help.
     
  3. sandrine10

    sandrine10 Power Member

    Joined:
    Apr 14, 2010
    Messages:
    638
    Likes Received:
    65
    Gender:
    Female
    Location:
    FreeLand
    the numbers are random ids(as 20897655 ) seems difficult to use what u suggest but in the other hand we can use regex pattern to find the string before the last slash as the url is the same before that.
    Any idea?
     
  4. jimbobo2779

    jimbobo2779 Jr. VIP Jr. VIP

    Joined:
    Sep 17, 2008
    Messages:
    3,635
    Likes Received:
    2,611
    Occupation:
    Software Engineer
    Location:
    UK
    Home Page:
    you just need the first 2 characters of the entire string to be different or the first 2 characters can be the same but not the rest of the string?

    How many characters the same at the beginning of the string is acceptable ?
     
  5. qrazy

    qrazy Senior Member

    Joined:
    Mar 19, 2012
    Messages:
    1,115
    Likes Received:
    1,723
    Location:
    Banana Republic
    You just need to use Regex to split the constant part (until the number after the last slash occurs) and the variable part( which is the number itself). Don't worry about how big the number is, Regex will take care of it.
     
    • Thanks Thanks x 1
  6. seeplusplus

    seeplusplus Power Member

    Joined:
    Aug 18, 2008
    Messages:
    517
    Likes Received:
    164
    Isn't regex a bit overkill?
     
  7. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    684
    Likes Received:
    326
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    why on earth would you use a regex?

    Not overkill, more wrong.


    What EXACTLY are you trying to do?


    Do you want to delete second line because it also starts with a "u" or because there are "url1" or because they will be URLs?

    So what you really want is a way to take a list of URLs and end up with a list of URLs where the domain only exists once?



    Get your lines into a list
    loop list
    get domain of line,
    if not processed before,
    add to a results list
    log domain


    Code:
    var list = getItemsIntoList()
    var listDomainsHandled = new  List<string>();
    var listResults = new List<string>();
    foreach(var line in list){
       var dom  = GetDomain(line);
       if(!listDomainsHandled .Contains(dom)){
            listResults.Add(line);
        }
    
    }
    
    
     
  8. Chdead

    Chdead Newbie

    Joined:
    Apr 12, 2015
    Messages:
    6
    Likes Received:
    0
    suppose that u have those links in a string array[]
    all you have to do is
    string firstletter="";
    for (int i =0;i<myarray.count;i++){
    if(firstletter == myarray.ToString().Substring(0,1))
    myarray.removeat(i);
    firstletter = myarray.ToString().Substring(0,1);
    }
    srry if there is some syntax errors i just wrote it fast here
     
  9. bitm0de

    bitm0de Newbie

    Joined:
    Jun 13, 2015
    Messages:
    12
    Likes Received:
    1
    That's what I was wondering... Why would anyone suggest regex for this? It's also slow for doing this task. If you stored links into some kind of list, you could easily remove the second string element.
     
  10. iDeveloper

    iDeveloper Newbie

    Joined:
    Sep 4, 2015
    Messages:
    14
    Likes Received:
    2
    Occupation:
    IM, Software Development
    Location:
    So Cal
    Home Page:
    This answer looks the best in terms of a workflow. But I guess you may still use Regex inside of GetDomain function to better recognize various protocol URL-s like http, https, ftp, etc
     
  11. winrocks

    winrocks Newbie

    Joined:
    Sep 18, 2015
    Messages:
    2
    Likes Received:
    0
    Home Page:
    lets expand it

    arl1/page1/title1/number1
    brl1/page1/title1/number2
    crl1/page1/title1/number1
    drl1/page1/title1/number2
    erl1/page1/title1/number1
    frl1/page1/title1/number2
    url1/page1/title1/number1 <start with "u"
    url1/page1/title1/number2 <start with "u", this line will be remove
    grl1/page1/title1/number1
    hrl1/page1/title1/number2
    irl1/page1/title1/number1
    jrl1/page1/title1/number2
    krl1/page1/title1/number1
    lrl1/page1/title1/number2

    1. Make a list (or array) of that line as string
    2. Loop that entire list
    3. Store the 'i' element and get its first char
    4. Store the 'i+1' element and get its first char
    5. Compare the first char of 'i' and 'i+1' element
    5. If same, remove 'i+1' element

    here how it look with code: (not excactly work with this code, just how it look)
    PHP:
    list() //an array that contains all that lines;
    var curentline //the 'i' element;
    var nextline // the 'i+1' element
    for (i=0< (list.count 1); i++){
        
    curentline = list(i);
        if (
    i+<= list.count-1){
            
    nextline = list(i+1);
            
    fisrtchar1 curentline(0); //get the fisr char of the curent line, you may need to convert string to array
            
    fisrtchar2 nextline(0); //get the fisr char of the next line, you may need to convert string to array
            
    if (fisrtchar1 == fisrtchar2){
                
    //remove the element list(i+1), this may effect the loop, so it better just to store the element info and remove it latter
            
    }
        }else{
            
    nextline = empty;
        }
    }