Get sub string from a string in MVC

Total Post:7

Points:49
C#
 455  View(s)
Ratings:
Rate this:

Hi,

I have faced some issue with my code. I want to show only 10 characters from my string.

Here is my code.

<span style="font-size: medium">

                            @if (item.Name.Length > 10)
                            {
                                @item.Name.ToString(); // Here i want to show only 10 characters.
                            }
                            else
                            {
                                @item.Name.ToString();
                            }
                        </span>

Please give me some idea to accomplish my task.


  1. Post:44

    Points:310
    Re: Show only some characters from a string in MVC

    Hi Aaditri,

                    I analyzed your code. You can use SubString() method and passed initially to max length in SubString() method to get only 10 characters.

    Please follow below a modified line of code to accomplish your task.

    <span style="font-size: medium">
    
                                @if (item.Name.Length > 10)
                                {
                                    @item.Name.Substring(0, 10).ToString();
                                }
                                else
                                {
                                    @item.Name.ToString();
                                }
                            </span>

    Hope its helpful ..!!


Answer