Reputation: 4079
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:
/ImageFolder/Gallery/AnAlbum/image.jpg
/ImageFolder/Gallery/AnAlbum/thumb/thumb_image.jpg
Upvotes: 1
Views: 1631
Reputation: 127543
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;
Upvotes: 4
Reputation: 700192
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_");
Upvotes: 0
Reputation: 3034
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();
}
}
Upvotes: 1
Reputation: 86074
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)
Upvotes: 1
Reputation: 156459
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;
Upvotes: 3
Reputation: 25370
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.
Upvotes: 2
Reputation: 5137
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;
Upvotes: 2