1

I use splice() function in JavaScript.

I couldn't find an equivalent method for ASP.NET. Is there any?

If not, how can I insert or remove an index of an array in ASP.NET?

I am trying to use the logic of this JavaScript code I've written once. Trying to do the same with ASP.NET, if that would help.

var imagePath = "/ImageFolder/Gallery/AnAlbum/image.jpg";
var folders = imagePath.split("/");
var imageFile = folders[folders.length - 1];
folders.splice(folders.length - 1, 1);
var newPath = folders.join("/");
var newImageUrl = newPath + "/thumb/thumb_" + imageFile;
return newImageUrl;

EDIT:

What is Splice?

From.

array.splice(start, deleteCount[, item1[, item2[, ...]]])

Parameters

start

Index at which to start changing the array. If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many elements from the end.

deleteCount

An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. In this case, you should specify at least one new element. If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted.

itemN

The element to add to the array. If you don't specify any elements, splice() will only remove elements from the array.

Returns

An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

Expected Output:

Fiddle

/ImageFolder/Gallery/AnAlbum/image.jpg

/ImageFolder/Gallery/AnAlbum/thumb/thumb_image.jpg

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
ilter
  • 4,030
  • 3
  • 34
  • 51
  • 1
    Is splice the same as `Split`? – Sayse May 11 '15 at 18:40
  • What is your expected result in your sample? – dotnetom May 11 '15 at 18:41
  • If you look at the js code I've provided, I use splice and split in different places. "Splice" is used to insert or remove an index of an array which I created by "split" function in the code. – ilter May 11 '15 at 18:42
  • My expected result is to have the same result I am having with my code, above :) OK, just sending a fiddler, it would make things easier for you to get what splice is :) – ilter May 11 '15 at 18:44
  • 1
    Here is an example of reproducing splice behavior in c# http://stackoverflow.com/questions/4402850/c-splicing-array – mjw May 11 '15 at 18:45
  • 1
    Possible [duplicate](http://stackoverflow.com/questions/406485/array-slices-in-c-sharp). It mentions to use `ArraySegment` – Peter Schneider May 11 '15 at 18:46

7 Answers7

4

Instead of trying to manipulate arrays (If you still want to, StriplingWarrior has a good answer showing you how) .NET has a larger toolset for working with paths directly. Your problem can be simplifed to

var imagePath = "/ImageFolder/Gallery/AnAlbum/image.jpg";
var folder = Path.GetDirectoryName(imagePath);
var imageFile = Path.GetFileName(imagePath);
var newImageUrl = folder + "/thumb/thumb_" + imageFile;
return newImageUrl;
Community
  • 1
  • 1
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • I like that you're solving the actual problem rather than answering the question. :-) – StriplingWarrior May 11 '15 at 18:51
  • We got a winner! Really, seeing the real problem and offering the solution OP wants... That is how you answer a question :) – ilter May 11 '15 at 18:54
  • The result is `"/thumb/thumb_image.jpg"`, not the expected `"/ImageFolder/Gallery/AnAlbum/thumb/thumb_image.jpg"`... – Guffa May 11 '15 at 18:56
  • He is combining the folder and the string to be added plus the filename. I think it gives the expected output. – ilter May 11 '15 at 19:00
  • I know it's silly but your original question was "How can I splice a string?" Not "How do I manipulate a file path?" It sounds nit picky as heck but when people search "How to splice a string?" An answer showing how to manipulate a file path using built-in .net tools isn't going to help them. – waltmagic May 11 '15 at 19:04
  • @ilter Guffa was right, the `Path.Combine` was wrong, I got rid of it and did a simple string concat instead. – Scott Chamberlain May 11 '15 at 19:06
  • Now the result is `\ImageFolder\Gallery\AnAlbum/thumb/thumb_image.jpg`. It changes the slashes to backslashes. – Guffa May 12 '15 at 00:01
  • @Guffa You are correct, If I could i would delete the answer but I can't because it is accepted. – Scott Chamberlain May 12 '15 at 00:05
3

Arrays in .NET have a fixed size, so there's no way to directly translate a splice() on an array, but if you use a List<> instead of an array, then there are Insert, Remove, and RemoveRange() methods that fulfill the same needs.

var imagePath = "/ImageFolder/Gallery/AnAlbum/image.jpg";
var folders = imagePath.Split('/').ToList(); // make it a list
var imageFile = folders[folders.Count - 1];
folders.RemoveAt(folders.Count - 1); // or folders.RemoveRange(folders.Count - 1, 1);

var newPath = string.Join("/", folders);
var newImageUrl = newPath + "/thumb/thumb_" + imageFile;
return newImageUrl;
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Actually, in the useage he is using, he is using splice to do a remove not a add. – Scott Chamberlain May 11 '15 at 18:45
  • Thank you! I was about to click you answer just a sec before I saw @ScottChamberlain 's answer. Good explanation with a valid example. – ilter May 11 '15 at 18:56
  • @ilter This is the most correct answer...Not trying to be nit picky but it is more helpful to all SO users if the answer matches the question – waltmagic May 11 '15 at 19:02
2

You may use List instead:

var imagePath = "/ImageFolder/Gallery/AnAlbum/image.jpg";
var folders = imagePath.Split('/');
var imageFile = folders[folders.Length - 1];
var foldersList = new List<String>(folders);
foldersList.RemoveAt(folders.Length - 1);
var newPath = String.Join("/", foldersList.ToArray());
var newImageUrl = newPath + "/thumb/thumb_" + imageFile;
return newImageUrl;
Mehrzad Chehraz
  • 5,092
  • 2
  • 17
  • 28
2

Use a List instead of an array. Then you can leverage List.Insert(index, item) and List.RemoveAt(index)

RemoveAt doesn't return you the value like splice does. But it doesn't look like you're using it anyways.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
1

You can use a regular expression to do the whole thing in one shot.

var imagePath = "/ImageFolder/Gallery/AnAlbum/image.jpg";
var newImageUrl = Regex.Replace(imagePath, "/(?!.*/)", "/thumb/thumb_");

Console.WriteLine (imagePath);
// ouput is /ImageFolder/Gallery/AnAlbum/image.jpg

Console.WriteLine (newImageUrl);
// output is /ImageFolder/Gallery/AnAlbum/thumb/thumb_image.jpg

The pattern is looking for the last slash. (a slash after which no additional slash appears)

recursive
  • 83,943
  • 34
  • 151
  • 241
  • Why use regex when .NET has the [`Path`](https://msdn.microsoft.com/en-us/library/System.IO.Path(v=vs.110).aspx) class for manipulating folder structure strings? – Scott Chamberlain May 11 '15 at 18:52
  • Using regex everywhere is not my thing. Make it a habbit and suffer in performance, imo. But, thanks for the answer. Hope that it will help someone :) – ilter May 11 '15 at 18:53
  • @ScottChamberlain: Because these strings look more like URLs than paths. And afaik, none of the path manipulation methods solve this problem as cleanly as this regex, but I'd be glad to be shown otherwise. – recursive May 11 '15 at 18:59
1

You can write your extension method for List.

public static class SpliceExtension
{
    public static List<T> Splice<T>(this List<T> list, int offset, int count)
    {
        return list.Skip(offset).Take(count).ToList();
    }
}
Kashif
  • 2,926
  • 4
  • 20
  • 20
0

You can't easily splice an array in .NET, as arrays are fixed size. The closest would be to create a new array and copy the items that you want to keep into it.

Instead of splitting the string, you can use the string manipulation methods to create the new string:

string imagePath = "/ImageFolder/Gallery/AnAlbum/image.jpg";

return imagePath.Insert(imagePath.LastIndexOf('/') + 1, "thumb/thumb_");
Guffa
  • 687,336
  • 108
  • 737
  • 1,005