asp.net-mvc 如何在MVC中的View中拆分字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24114849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:43:17  来源:igfitidea点击:

How to split string in View in MVC?

asp.net-mvcstringmodel-view-controllerviewsplit

提问by geekforfreek

In my View I have this condition:

在我看来,我有这个条件:

@if (User.Identity.Name == "abc")
{
   ... do this!
}

How can I split this string "User.Identity.Name" in View (in MVC) so I can create new condition, something like this:

如何在视图中(在 MVC 中)拆分此字符串“User.Identity.Name”,以便我可以创建新条件,如下所示:

string last = User.Identity.Name.Substring(User.Identity.Name.LastIndexOf('.') + 1);
if (last == "this")
{
   ... do this!
}

thanks.

谢谢。

回答by Ehsan Sajjad

you can do it like this:

你可以这样做:

@{

    var temp= User.Identity.Name.Split('.');

    if (temp[temp.Count() - 1] == "this")
    {

    }

}

or if "."will be only one time in that string then you can hardcode like this:

或者如果 “。” 在该字符串中只会出现一次,然后您可以像这样进行硬编码:

@{

    var temp= User.Identity.Name.Split('.');

        if (temp[1] == "this")
        {

        }
}

回答by Karibasappa G C

see below example , which finds last word of a string after last dot in it.

请参见下面的示例,它在字符串的最后一个点之后查找字符串的最后一个单词。

String str = "abc.def.xyz";

String last = str.substring(str.lastIndexOf('.')+1);
System.out.println(last);
if(last.equals("something")){
    //do this
}

else{
    //do this
}

here lastcomes as xyz

这里lastxyz