Link to home
Start Free TrialLog in
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

asked on

Substring in razor view

Hi Experts,

I have this code.

@if (WorkContext.CurrentCulture == "de-DE"){
}

Open in new window

WorkContext.CurrentCulture can have the value de-DE, fr-FR or it-IT.

How can I substring and get only DE ,FR and IT?  This code is in the view.


Thanks in advance!


Avatar of louisfr
louisfr

If CurrentCulture is a String, you can call SubString(3, 2) on it, or Split('-') and check second item in the resulting array.
ASKER CERTIFIED SOLUTION
Avatar of Kelvin McDaniel
Kelvin McDaniel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Kelvin McDaniel Calling Last() on a array will run an operation on it while accessing the last element of that specific array by index would be O(1) instead of O(n). Just thought i'd let you know 🙂

@RadhaKrishnaKiJaya Your string comparison isn't correct from my recollection, you're actually comparing reference-type instead of value-type. You should be using Equals() method instead.
@Jonathan Dahan yep, I’m aware. However, there’s no guarantee that there will only be two elements in all of those possible strings. And the expense of running .Last() on a single instance of a two item array is relatively inconsequential. Just thought I’d let you know. 😊
@Jonathan Dahan
  • Calling Last() on an array is an O(1) operation.
  • In C#, the String class overloads the equality operator (which calls the static String.Equals method).
@louisfr I know what i'm talking about, and calling Last() on an array will cause the operation call Count() which runs over the entire sequence in order to find out how many elements there are, and then get the last index which is the entire count. This is an O(n) operation. But, on a enumerable that implements IList<T> it will be O(1) because of the Count property which is O(1) because the fact that the implementation of the enumerable keeps updating the counter in real time. Read more here.

Also, about your 2nd statement, look here to see why you're wrong again. If there's a new update on this case from Microsoft then let me know, i'd be happy to know about it.

I don't know what caused you to write the opposite of everything I said, they're just invalid statements.
@Jonathan Dahan 
on a enumerable that implements IList<T>
Arrays of T implement IList<T>
about your 2nd statement
WorkContext.CurrentCulture is not typed as string?
Fair enough ✌️