Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 388
geekforfreek Avatar asked Nov 01 '25 15:11

geekforfreek


2 Answers

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

        }
}
like image 69
Ehsan Sajjad Avatar answered Nov 04 '25 07:11

Ehsan Sajjad


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

like image 29
Karibasappa G C Avatar answered Nov 04 '25 06:11

Karibasappa G C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!