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> & </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.
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]); }
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?
Only the word Split be within the <span>
I did try something like this but and getting Split again
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:
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.
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
The "&" will have a different CSS style and will always be represent there only the Header Content will change most of the time.
But thinking about it yes this could work and making use of HTMl.Raw
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();
is working on a reply...