geekforfreek
geekforfreek

Reputation: 146

How to split string in View in MVC?

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:

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

thanks.

Upvotes: 4

Views: 37103

Answers (2)

Karibasappa G C
Karibasappa G C

Reputation: 2732

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 last comes as xyz

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

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")
        {

        }
}

Upvotes: 10

Related Questions