2sxc |通过使用 Javascript 方法修剪来删除文件路径 [英] 2sxc | removing file path by trimming w/ Javascript method
本文介绍了2sxc |通过使用 Javascript 方法修剪来删除文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我显示了以下文件路径:
I have the following file path displaying:
而且我只想显示文件名Doc1"(减去路径和扩展名).
And I want to display only file name 'Doc1' (minus path and extension).
我尝试了以下操作但未成功,如果您能进一步了解我做错了什么,我将不胜感激...
I have tried unsucessfully the following and would appreciate any further light you could share as to what I am doing wrong...
@functions{
public static string SplitWord(string text, int length)
{
string str = text;
int n = str.LastIndexOf(".");
string str1=str.Substring(n,str.LastIndexOf("/"));
str1=str1.Substring(1,str1.Length);
return str1;
}
}
<ol>
@foreach (var q in AsDynamic(App.Data["CatFilter"]))
{
<li class="sc-element faq-set faq-setOne" data-tags="@String.Join(",", ((List<DynamicEntity>)q.Categories).Select(a => AsDynamic(a).EntityId))">
@q.Toolbar @Edit.Toolbar(actions: "edit,new", contentType: "CatFilter")
<a class="faq-question" style="cursor: pointer">
@if(!String.IsNullOrEmpty(q.LinkText))
{
SplitWord(@q.LinkText,@q.LinkText.Length);
} else {
SplitWord(@q.Link,@q.Link.Length);
}
</a>
</li>
}
</ol>
我也在 IF 条件下尝试了以下变体,但还是没有运气.
I have also tried variations of the following within the IF condition but again no luck.
,,,,@:var str = q.Link;
,,,,@:var n = str.lastIndexOf(".");
,,,,@:var str1=str.Substring(n,str.lastIndexOf("/"))
,,,,@:str1=str1.Substring(1,str1.Length);
谢谢,
推荐答案
您实际上只是以错误的方式使用 Substring.你可能想要
You're actually just using Substring the wrong way. You probably want
public static string SplitWord(string text, int length)
{
int slash = text.LastIndexOf("/");
int dot = text.LastIndexOf(".");
return text.Substring(slash + 1, dot - slash);
}
试一试 - 可能需要对其中一个值进行另一个 +1 或 -1,但这应该可以解决问题.
Give it a try - might need another +1 or -1 on one of the values, but that should do the trick.
这篇关于2sxc |通过使用 Javascript 方法修剪来删除文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文