Tag Info

New answers tagged

-1

In Smalltalk it is straightforward to define the / method in String so that it works like this: 'assets' / 'sounds' => 'assets/sounds'. 'assets/' / 'sounds' => 'assets/sounds'. 'assets' / '/sounds' => 'assets/sounds'. 'assets/' / '/sounds' => 'assets/sounds'. Here is a simple implementation of the method (you can make it better): / aString ...


18

Nearly every major programming language has a library to handle the directory separators for you. You should leverage them. This will simplify your code and prevent bugs. In my experience, the usual reason for combining strings like this is that they come from different sources. Sometimes it's different pieces from a configuration file. Sometimes it's a ...


5

Folders and files differ only in one aspect: folders end with a slash where files do not. Furthermore, absolute paths begin with a / where relative paths do not. If you use this consistently concatenating paths and files together should be no problem. var absolutepath = "/my/path"; var relativepath = "css/"; var filename = "test.css"; var ...


21

Note that in .NET you should use the Path.Combine method. var path = System.IO.Path.Combine("assets", "sounds"); The reason for this is that it 'knows' the correct characters to be used when constructing the folder names. This takes away the 'problem' of pre or post fixing.


4

When building paths I often use a function that adds the trailing slash if it isn't already there. Then paths can be built like: filename := fs( 'assets') + fs( 'images') + fs( 'icons') + 'some.png'; where fs() adds a trailing slash if its needed.


0

My personal preference is this: var assets = "/images" var sounds = assets+"/sounds" I always use absolute paths (/images/...), it feels less prone to error, to me. It is also more fool proof to use var sounds = assets+"/sounds" because even if assets had a trailing slash and you ended up with /images//sounds, it would still resolve to /images/sounds. ...


37

In Java, the answer would be "neither of the above". Best practice would be to assemble pathnames using the java.io.File class; e.g. File assets = new File("images"); File sounds = new File(assets, "sounds"); The File class also takes care of platform-specific pathname separators. There is a separate issue of whether your pathname should start with a ...


4

I think there is no magic or "common practice" on how to implement paths, but certainly string concatenation is not the way to go. You can develop your own API for dealing with cases, but it may require some effort. In particular, you should be careful about different platforms. For example, in Windows \ is the separator while in Unix-based systems / is the ...



Top 50 recent answers are included