Search code examples
javascriptrazor2sxc

2sxc | removing file path by trimming w/ Javascript method


I have the following file path displaying: enter image description here

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>

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);

Thx,


Solution

  • 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);
    }
    

    Give it a try - might need another +1 or -1 on one of the values, but that should do the trick.