Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Fuji Kusaka 2203 posts 4220 karma points
    May 19, 2014 @ 15:58
    Fuji Kusaka
    0

    Splitting TextString in 3 for better formatting

    Hi,

    Is there a way of splitting value of a textString in 3 parts to achieve something like this in html?

    Header <span> & </span> Only Title

    Only the word Split be within the <span>

    I did try something like this but and getting Split again

    var titlePage = Model.optionalHeading;
    var tit = titlePage.IndexOf(' ');
    @if(tit != -1){
    
    @titlePage.Substring(0,tit) <span>@titlePage.Substring(3,tit)</span>@titlePage.Substring(tit,titlePage.Length-tit)
    }
    
    Header <span> & </span> & Only Title  
    
  • Peter Duncanson 430 posts 1360 karma points c-trib
    May 19, 2014 @ 16:20
    Peter Duncanson
    0

    Going to need a bit more info I think to get to the bottom of it.

    Guessing  a bit you could a simply replacing the text with itself plus your new HTML:

    formattedTitlePage = titlePage.Replace( " & ", "<span> &amp; </span>" )

    More info means a better answer :)

    Depends how robust you need it to be too. Are we always looking for a " ", are you wanting to replace the existing "&" with the span wrapped version? Etc.

  • Fuji Kusaka 2203 posts 4220 karma points
    May 19, 2014 @ 17:38
    Fuji Kusaka
    0

    Hi Peter,

    Basically i have a textString where there will be a special formatting after the first space within the value in the textString.

    Saying so after the first space lets say 

    <div class="header">Header <span>&</span> Header Content</div>

    The "&" will have a different CSS style and will always be represent there only the Header Content will change most of the time.

     

     

  • Fuji Kusaka 2203 posts 4220 karma points
    May 19, 2014 @ 17:44
    Fuji Kusaka
    0

    But thinking about it yes this could work and making use of HTMl.Raw

    formattedTitlePage = Html.Raw(titlePage.Replace(" & ","<span> &amp; </span>"))
  • Chris Lord 15 posts 51 karma points
    May 19, 2014 @ 18:01
    Chris Lord
    0

    The replace would work fine, it you wanted to replace all & in the string with the span wrapped around it. What if the word after the first space is not &? Would you still want to wrap it in a span? If so, can't you split on the space, and then loop through the array and output the string with the span tag around the second item in the array?

    May have misunderstood what you want to do, so apologies if I have :-), but your posted suggested you want to replace the second word in the string, whether it's an & or not.

    "where there will be a special formatting after the first space within the value"

    Something like this, you could create an extension method to keep things tidier;

    var heading = "Some Content & Some Value";
    var arr = heading.Split(' ');
    var output = "";

    for (int i = 0; i < arr.Length; i++)
    {
    if (i == 1)
    output += string.Format("<span>{0}</span> ", arr[i]);
    else
    output += string.Format("{0} ", arr[i]);
    }

    Console.WriteLine(output.Trim());
    Console.Read();

     

Please Sign in or register to post replies

Write your reply to:

Draft