Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

razor string manipulation

Tags:

asp.net

razor

I have a razor string

 @postername.Substring(0, @postername.IndexOf("@"))

If the username has email I get the username before @ sign but if the username doesn't have email I want to have that whole word, how to do ?

 if(@postername.Contains("@")){
             @postername.Substring(0, @postername.IndexOf("@"))
            }else{
            @postername
            }

but didn't work, pls help

like image 851
ktm Avatar asked Jun 03 '26 18:06

ktm


1 Answers

If you must do this at View level, build the logic into a variable first:

@{

 string posternameShort = postername;

 if(postername.Contains("@")){
     posternameShort = postername.Substring(0, postername.IndexOf("@"))
 }

}

Then call the new variable:

@posternameShort
like image 84
Curtis Avatar answered Jun 07 '26 23:06

Curtis