Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

C# 8 - use Range/Index instead of Substring()

KrzysztofPajak0109debdcc37
diff --git a/src/Business/Grand.Business.Checkout/Services/GiftVouchers/GiftVoucherService.cs b/src/Business/Grand.Business.Checkout/Services/GiftVouchers/GiftVoucherService.cs
index 454d3288e..0c2d23bee 100644
--- a/src/Business/Grand.Business.Checkout/Services/GiftVouchers/GiftVoucherService.cs
+++ b/src/Business/Grand.Business.Checkout/Services/GiftVouchers/GiftVoucherService.cs
@@ -166,11 +166,7 @@ namespace Grand.Business.Checkout.Services.GiftVouchers
/// <returns>Result</returns>
public virtual string GenerateGiftVoucherCode()
{
- int length = 8;
- string result = Guid.NewGuid().ToString();
- if (result.Length > length)
- result = result.Substring(0, length);
- return result;
+ return Guid.NewGuid().ToString()[..8];
}
#endregion
diff --git a/src/Business/Grand.Business.Common/Services/Security/EncryptionService.cs b/src/Business/Grand.Business.Common/Services/Security/EncryptionService.cs
index 6800269ea..a5f29ca82 100644
--- a/src/Business/Grand.Business.Common/Services/Security/EncryptionService.cs
+++ b/src/Business/Grand.Business.Common/Services/Security/EncryptionService.cs
@@ -72,8 +72,8 @@ namespace Grand.Business.Common.Services.Security
var tDES = TripleDES.Create();
- tDES.Key = new ASCIIEncoding().GetBytes(privateKey.Substring(0, 24));
- tDES.IV = new ASCIIEncoding().GetBytes(privateKey.Substring(16, 8));
+ tDES.Key = new ASCIIEncoding().GetBytes(privateKey);
+ tDES.IV = new ASCIIEncoding().GetBytes(privateKey[^8..]);
byte[] encryptedBinary = EncryptTextToMemory(plainText, tDES.Key, tDES.IV);
return Convert.ToBase64String(encryptedBinary);
@@ -94,8 +94,8 @@ namespace Grand.Business.Common.Services.Security
throw new Exception("Wrong encryp private key");
var tDES = TripleDES.Create();
- tDES.Key = new ASCIIEncoding().GetBytes(encryptionPrivateKey.Substring(0, 24));
- tDES.IV = new ASCIIEncoding().GetBytes(encryptionPrivateKey.Substring(16, 8));
+ tDES.Key = new ASCIIEncoding().GetBytes(encryptionPrivateKey);
+ tDES.IV = new ASCIIEncoding().GetBytes(encryptionPrivateKey[^8..]);
byte[] buffer = Convert.FromBase64String(cipherText);
return DecryptTextFromMemory(buffer, tDES.Key, tDES.IV);
diff --git a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidExtensions.cs b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidExtensions.cs
index 6cff215d1..dd1715765 100644
--- a/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidExtensions.cs
+++ b/src/Business/Grand.Business.Messages/DotLiquidDrops/LiquidExtensions.cs
@@ -15,7 +15,7 @@ namespace Grand.Business.Messages.DotLiquidDrops
foreach (var drop in drops)
{
toReturn.AddRange(drop.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
- .Select(x => "{{" + drop.Name.Substring(6, drop.Name.Length - 6) + "." + x.Name + "}}"));
+ .Select(x => "{{" + drop.Name[6..] + "." + x.Name + "}}"));
}
return toReturn;
diff --git a/src/Business/Grand.Business.Storage/Services/FileSystemStore.cs b/src/Business/Grand.Business.Storage/Services/FileSystemStore.cs
index efb13fe2c..2b8e1ab90 100644
--- a/src/Business/Grand.Business.Storage/Services/FileSystemStore.cs
+++ b/src/Business/Grand.Business.Storage/Services/FileSystemStore.cs
@@ -79,7 +79,7 @@ namespace Grand.Business.Storage.Services
.Select(f =>
{
var fileSystemInfo = new PhysicalDirectoryInfo(new DirectoryInfo(f));
- var fileRelativePath = f.Substring(_fileSystemPath.Length);
+ var fileRelativePath = f[_fileSystemPath.Length..];
var filePath = this.NormalizePath(fileRelativePath);
return new FileSystemStoreEntry(filePath, fileSystemInfo);
}));
@@ -92,7 +92,7 @@ namespace Grand.Business.Storage.Services
.Select(f =>
{
var fileSystemInfo = new PhysicalFileInfo(new FileInfo(f));
- var fileRelativePath = f.Substring(_fileSystemPath.Length);
+ var fileRelativePath = f[_fileSystemPath.Length..];
var filePath = this.NormalizePath(fileRelativePath);
return new FileSystemStoreEntry(filePath, fileSystemInfo);
}));
diff --git a/src/Business/Grand.Business.Storage/Services/FileSystemStoreEntry.cs b/src/Business/Grand.Business.Storage/Services/FileSystemStoreEntry.cs
index 4be95ddf7..0ae61e61b 100644
--- a/src/Business/Grand.Business.Storage/Services/FileSystemStoreEntry.cs
+++ b/src/Business/Grand.Business.Storage/Services/FileSystemStoreEntry.cs
@@ -18,7 +18,7 @@ namespace Grand.Business.Storage.Services
public string Path => _path;
public string Name => _fileInfo.Name;
- public string DirectoryPath => _path.Substring(0, _path.Length - Name.Length).TrimEnd('/');
+ public string DirectoryPath => _path[0..(_path.Length - Name.Length)].TrimEnd('/');
public string PhysicalPath => _fileInfo.PhysicalPath;
public DateTime LastModifiedUtc => _fileInfo.LastModified.UtcDateTime;
public long Length => _fileInfo.Length;
diff --git a/src/Core/Grand.SharedKernel/Extensions/CommonHelper.cs b/src/Core/Grand.SharedKernel/Extensions/CommonHelper.cs
index ece9c2287..bd636fd9d 100644
--- a/src/Core/Grand.SharedKernel/Extensions/CommonHelper.cs
+++ b/src/Core/Grand.SharedKernel/Extensions/CommonHelper.cs
@@ -77,10 +77,10 @@ namespace Grand.SharedKernel.Extensions
RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
var byteArray = new byte[length];
provider.GetBytes(byteArray);
- for (int i = 0; i < length; i++)
- str = String.Concat(str, byteArray[i].ToString());
+ for (var i = 0; i < length; i++)
+ str = string.Concat(str, byteArray[i].ToString());
- return str.Substring(0, length);
+ return str[..length];
}
/// <summary>
@@ -111,7 +111,7 @@ namespace Grand.SharedKernel.Extensions
if (str.Length > maxLength)
{
var pLen = postfix == null ? 0 : postfix.Length;
- var result = str.Substring(0, maxLength - pLen);
+ var result = str[..(maxLength - pLen)];
if (!String.IsNullOrEmpty(postfix))
{
result += postfix;
diff --git a/src/Web/Grand.Web.Admin/Controllers/OrderController.cs b/src/Web/Grand.Web.Admin/Controllers/OrderController.cs
index 3167f95d3..0aadc3ae6 100644
--- a/src/Web/Grand.Web.Admin/Controllers/OrderController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/OrderController.cs
@@ -687,7 +687,7 @@ namespace Grand.Web.Admin.Controllers
string orderItemId = "";
foreach (var formValue in form.Keys)
if (formValue.StartsWith("btnSaveOrderItem", StringComparison.OrdinalIgnoreCase))
- orderItemId = formValue.Substring("btnSaveOrderItem".Length);
+ orderItemId = formValue["btnSaveOrderItem".Length..];
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId);
if (orderItem == null)
@@ -761,7 +761,7 @@ namespace Grand.Web.Admin.Controllers
string orderItemId = "";
foreach (var formValue in form.Keys)
if (formValue.StartsWith("btnDeleteOrderItem", StringComparison.OrdinalIgnoreCase))
- orderItemId = formValue.Substring("btnDeleteOrderItem".Length);
+ orderItemId = formValue["btnDeleteOrderItem".Length..];
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId);
if (orderItem == null)
@@ -799,7 +799,7 @@ namespace Grand.Web.Admin.Controllers
string orderItemId = "";
foreach (var formValue in form.Keys)
if (formValue.StartsWith("btnCancelOrderItem", StringComparison.OrdinalIgnoreCase))
- orderItemId = formValue.Substring("btnCancelOrderItem".Length);
+ orderItemId = formValue["btnCancelOrderItem".Length..];
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId);
if (orderItem == null)
@@ -836,7 +836,7 @@ namespace Grand.Web.Admin.Controllers
string orderItemId = "";
foreach (var formValue in form.Keys)
if (formValue.StartsWith("btnResetDownloadCount", StringComparison.OrdinalIgnoreCase))
- orderItemId = formValue.Substring("btnResetDownloadCount".Length);
+ orderItemId = formValue["btnResetDownloadCount".Length..];
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId);
if (orderItem == null)
@@ -877,7 +877,7 @@ namespace Grand.Web.Admin.Controllers
string orderItemId = "";
foreach (var formValue in form.Keys)
if (formValue.StartsWith("btnPvActivateDownload", StringComparison.OrdinalIgnoreCase))
- orderItemId = formValue.Substring("btnPvActivateDownload".Length);
+ orderItemId = formValue["btnPvActivateDownload".Length..];
var orderItem = order.OrderItems.FirstOrDefault(x => x.Id == orderItemId);
if (orderItem == null)
diff --git a/src/Web/Grand.Web.Admin/Controllers/PluginController.cs b/src/Web/Grand.Web.Admin/Controllers/PluginController.cs
index 240cfed07..f57ab1b21 100644
--- a/src/Web/Grand.Web.Admin/Controllers/PluginController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/PluginController.cs
@@ -170,7 +170,7 @@ namespace Grand.Web.Admin.Controllers
string systemName = null;
foreach (var formValue in form.Keys)
if (formValue.StartsWith("install-plugin-link-", StringComparison.OrdinalIgnoreCase))
- systemName = formValue.Substring("install-plugin-link-".Length);
+ systemName = formValue["install-plugin-link-".Length..];
var pluginInfo = PluginManager.ReferencedPlugins.FirstOrDefault(x => x.SystemName == systemName);
if (pluginInfo == null)
@@ -212,7 +212,7 @@ namespace Grand.Web.Admin.Controllers
string systemName = null;
foreach (var formValue in form.Keys)
if (formValue.StartsWith("uninstall-plugin-link-", StringComparison.OrdinalIgnoreCase))
- systemName = formValue.Substring("uninstall-plugin-link-".Length);
+ systemName = formValue["uninstall-plugin-link-".Length..];
var pluginInfo = PluginManager.ReferencedPlugins.FirstOrDefault(x => x.SystemName == systemName);
if (pluginInfo == null)
@@ -255,7 +255,7 @@ namespace Grand.Web.Admin.Controllers
string systemName = null;
foreach (var formValue in form.Keys)
if (formValue.StartsWith("remove-plugin-link-", StringComparison.OrdinalIgnoreCase))
- systemName = formValue.Substring("remove-plugin-link-".Length);
+ systemName = formValue["remove-plugin-link-".Length..];
var pluginInfo = PluginManager.ReferencedPlugins.FirstOrDefault(x => x.SystemName == systemName);
if (pluginInfo == null)
@@ -436,7 +436,7 @@ namespace Grand.Web.Admin.Controllers
if (pluginInfo.SupportedVersion == GrandVersion.SupportedPluginVersion)
{
supportedVersion = true;
- _fpath = entry.FullName.Substring(0, entry.FullName.LastIndexOf("/"));
+ _fpath = entry.FullName[..entry.FullName.LastIndexOf("/")];
archive.Entries.Where(x => !x.FullName.Contains(_fpath)).ToList()
.ForEach(y => { archive.GetEntry(y.FullName).Delete(); });
@@ -455,7 +455,7 @@ namespace Grand.Web.Admin.Controllers
throw new Exception($"This plugin doesn't support the current version - {GrandVersion.SupportedPluginVersion}");
else
{
- var pluginname = _fpath.Substring(_fpath.LastIndexOf('/') + 1);
+ var pluginname = _fpath[(_fpath.LastIndexOf('/') + 1)..];
var _path = "";
var entries = archive.Entries.ToArray();
diff --git a/src/Web/Grand.Web.Admin/Controllers/RoxyFilemanController.cs b/src/Web/Grand.Web.Admin/Controllers/RoxyFilemanController.cs
index 08a0f8f57..e60f3e480 100644
--- a/src/Web/Grand.Web.Admin/Controllers/RoxyFilemanController.cs
+++ b/src/Web/Grand.Web.Admin/Controllers/RoxyFilemanController.cs
@@ -188,10 +188,10 @@ namespace Grand.Web.Admin.Controllers
return result;
if (json.StartsWith("{"))
- json = json.Substring(1, json.Length - 2);
+ json = json[1..^1];
json = json.Trim();
- json = json.Substring(1, json.Length - 2);
+ json = json = json[1..^1];
var lines = Regex.Split(json, "\"\\s*,\\s*\"");
foreach (var line in lines)
diff --git a/src/Web/Grand.Web.Common/TagHelpers/Admin/Extend/DefaultHtmlGenerator.cs b/src/Web/Grand.Web.Common/TagHelpers/Admin/Extend/DefaultHtmlGenerator.cs
index cf16a8e70..ffa214b54 100644
--- a/src/Web/Grand.Web.Common/TagHelpers/Admin/Extend/DefaultHtmlGenerator.cs
+++ b/src/Web/Grand.Web.Common/TagHelpers/Admin/Extend/DefaultHtmlGenerator.cs
@@ -438,7 +438,7 @@ namespace Grand.Web.Common.TagHelpers.Admin
}
else
{
- resolvedLabelText = expression.Substring(index + 1);
+ resolvedLabelText = expression[(index + 1)..];
}
}
diff --git a/src/Web/Grand.Web/Controllers/CommonController.cs b/src/Web/Grand.Web/Controllers/CommonController.cs
index 8c727d8c9..5214730c1 100644
--- a/src/Web/Grand.Web/Controllers/CommonController.cs
+++ b/src/Web/Grand.Web/Controllers/CommonController.cs
@@ -76,7 +76,7 @@ namespace Grand.Web.Controllers
url = WebUtility.UrlDecode(resultpath);
url = url.TrimStart('/');
- var result = url.Contains('/') ? url.Substring(url.IndexOf('/')) : string.Empty;
+ var result = url.Contains('/') ? url[url.IndexOf('/')..] : string.Empty;
result = pathBase + result;
return result;
diff --git a/src/Web/Grand.Web/Features/Handlers/Blogs/GetHomePageBlogHandler.cs b/src/Web/Grand.Web/Features/Handlers/Blogs/GetHomePageBlogHandler.cs
index 998507fb7..d9f328a16 100644
--- a/src/Web/Grand.Web/Features/Handlers/Blogs/GetHomePageBlogHandler.cs
+++ b/src/Web/Grand.Web/Features/Handlers/Blogs/GetHomePageBlogHandler.cs
@@ -69,7 +69,7 @@ namespace Grand.Web.Features.Handlers.Blogs
var description = post.GetTranslation(x => x.BodyOverview, _workContext.WorkingLanguage.Id);
item.SeName = post.GetSeName(_workContext.WorkingLanguage.Id);
item.Title = post.GetTranslation(x => x.Title, _workContext.WorkingLanguage.Id);
- item.Short = description?.Length > _blogSettings.MaxTextSizeHomePage ? description.Substring(0, _blogSettings.MaxTextSizeHomePage) : description;
+ item.Short = description?.Length > _blogSettings.MaxTextSizeHomePage ? description[.._blogSettings.MaxTextSizeHomePage] : description;
item.CreatedOn = _dateTimeService.ConvertToUserTime(post.StartDateUtc ?? post.CreatedOnUtc, DateTimeKind.Utc);
item.UserFields = post.UserFields;
item.Category = (await _blogService.GetBlogCategoryByPostId(post.Id)).FirstOrDefault()?.GetTranslation(x => x.Name, _workContext.WorkingLanguage.Id);