Click here to Skip to main content
15,887,596 members
Home / Discussions / C#
   

C#

 
QuestionPath stripping algorithim.....help required Pin
c#_keithy10-Sep-07 4:37
c#_keithy10-Sep-07 4:37 
AnswerRe: Path stripping algorithim.....help required Pin
Judah Gabriel Himango10-Sep-07 4:59
sponsorJudah Gabriel Himango10-Sep-07 4:59 
AnswerRe: Path stripping algorithim.....help required Pin
Skippums10-Sep-07 5:15
Skippums10-Sep-07 5:15 
GeneralRe: Path stripping algorithim.....help required Pin
c#_keithy10-Sep-07 5:43
c#_keithy10-Sep-07 5:43 
GeneralRe: Path stripping algorithim.....help required Pin
Skippums10-Sep-07 5:48
Skippums10-Sep-07 5:48 
GeneralRe: Path stripping algorithim.....help required Pin
Guffa10-Sep-07 6:04
Guffa10-Sep-07 6:04 
GeneralRe: Path stripping algorithim.....help required Pin
Skippums10-Sep-07 7:06
Skippums10-Sep-07 7:06 
AnswerRe: Path stripping algorithim.....help required Pin
Luc Pattyn10-Sep-07 7:12
sitebuilderLuc Pattyn10-Sep-07 7:12 
Hi,

there is a lot of opportunity for optimizations here;
I assume you are not interested in partial folder names (i.e. C:\A\B1 and C:\A\B2 have
C:\A in common, not C:\A\B).

The candidate result initially equals the first string.
The only thing that can happen to it is you must drop everything from the last backslash
up to the end (possibly performed iteratively).

Something like:
public string findCommonPath(string[] strings) {
    string common=strings[strings.Length-1];
    foreach(string str in strings) {
        while (!str.StartsWith(common)) {
            int i=common.LastIndefOf(@"\");
            if (i<=0) return null; // nothing left
            common=common.SubString(0,i);    
        }
    }
    return common;
}


remarks:
- you may want to do everything in case-insensitive way;
- if your strings happen to be sorted alphabetically, you should only compare the first
and last one !
- I started with the last string hoping it differs most from the first one, resulting
in a sooner shortening of common, hence less CPU cycles for StartsWith.
- if you happen to know what the shortest string is, you may want to handle that one first.
- the number of objects created is very low (in the order of the number of backslashes
in the first string)

Smile | :)

Luc Pattyn [Forum Guidelines] [My Articles]


this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google


GeneralRe: Path stripping algorithim.....help required Pin
Skippums10-Sep-07 8:22
Skippums10-Sep-07 8:22 
GeneralRe: Path stripping algorithim.....help required Pin
Luc Pattyn10-Sep-07 9:53
sitebuilderLuc Pattyn10-Sep-07 9:53 
GeneralRe: Path stripping algorithim.....help required Pin
Skippums10-Sep-07 10:08
Skippums10-Sep-07 10:08 
GeneralRe: Path stripping algorithim.....help required Pin
c#_keithy10-Sep-07 21:57
c#_keithy10-Sep-07 21:57 
QuestionHelp with DataGridViewTextBoxColumn Pin
gugulethun10-Sep-07 3:47
gugulethun10-Sep-07 3:47 
AnswerRe: Help with DataGridViewTextBoxColumn Pin
mr.mohsen10-Sep-07 4:57
mr.mohsen10-Sep-07 4:57 
QuestionWindow Service Error Pin
ramdil10-Sep-07 2:56
ramdil10-Sep-07 2:56 
AnswerRe: Window Service Error Pin
Nissim Salomon10-Sep-07 3:43
Nissim Salomon10-Sep-07 3:43 
GeneralRe: Window Service Error Pin
ramdil12-Sep-07 23:41
ramdil12-Sep-07 23:41 
QuestionNeed help with time math calculation Pin
RapGame200610-Sep-07 2:38
RapGame200610-Sep-07 2:38 
AnswerRe: Need help with time math calculation Pin
Colin Angus Mackay10-Sep-07 2:41
Colin Angus Mackay10-Sep-07 2:41 
GeneralRe: Need help with time math calculation Pin
RapGame200610-Sep-07 4:52
RapGame200610-Sep-07 4:52 
Questionreterive Xml attribute values [modified] Pin
M.A.Burhan10-Sep-07 2:14
M.A.Burhan10-Sep-07 2:14 
AnswerRe: reterive Xml attribute values Pin
pmarfleet10-Sep-07 2:47
pmarfleet10-Sep-07 2:47 
QuestionChat program for Wan Pin
sandipan.neogi@gmail.com10-Sep-07 2:09
sandipan.neogi@gmail.com10-Sep-07 2:09 
AnswerRe: Chat program for Wan Pin
c0ax_lx10-Sep-07 4:47
c0ax_lx10-Sep-07 4:47 
QuestionHow to installl setup.exe for every machine Pin
ASysSolvers10-Sep-07 1:15
ASysSolvers10-Sep-07 1:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.