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

sandrine10

Power Member
Joined
Apr 14, 2010
Messages
779
Reaction score
88
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:
You might need to use Regex pattern to find the numbers after last trailing slash. Post your code snippet, you might get better help.
 
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?
 
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 ?
 
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?

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.
 
why on earth would you use a regex?

Isn't regex a bit overkill?
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);
    }

}
 
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
 
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.
 
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);
    }

}

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
 
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?

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; i < (list.count - 1); i++){
    curentline = list(i);
    if (i+1 <= 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;
    }
}
 
Back
Top