Reputation: 306
I am wanting to know how to find specific words in a text label and make them bold.
For example descriptionTxt.Text
contains several words 'Step 1:, Step 2: and so on ...
I want to find the literal text "Step", followed by a space, then one or more digits, followed by a colon" and make this bold.
C# Code
protected void Page_Load(object sender, EventArgs e)
{
string ID = Request.QueryString["id"];
RecipeID.Text = ID;
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
con.Open();
try
{
SqlDataAdapter sda = new SqlDataAdapter("Select Recipe_Name, Recipe_Description, Recipe_Instructions FROM Recipe Where Recipe_ID= @recipeid", con);
sda.SelectCommand.Parameters.Add("@recipeid", SqlDbType.Int).Value = RecipeID.Text;
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
nameTxt.Text = dt.Rows[0][0].ToString();
descriptionTxt.Text = dt.Rows[0][1].ToString();
instructionsTxt.Text = dt.Rows[0][2].ToString();
dt.Clear();
}
catch (Exception ex)
{
}
con.Close();
}
Upvotes: 2
Views: 2197
Reputation: 151586
The pattern you describe lends itself for a regular expression.
In this case the expression (Step [0-9]+:)
, meaning "The literal text "Step", followed by a space, then one or more digits, followed by a colon":
var stepRegex = new Regex("(Step [0-9]+:)");
Now to "make bold" as you ask, you can use HTML as we can assume an HTML context from your ASP.NET tag. This is as trivial as wrapping the found value with <b></b>
tags, using $1
to refer to the matched group.
string content = "Step 1: do something. Step 2: see step 1.";
string replaced = stepRegex.Replace(content, "<b>$1</b>");
This will yield "<b>Step 1:</b> do something. <b>Step 2:</b> see step 1."
.
Also, you don't want to assign this HTML to a Label control, use a Literal instead.
Upvotes: 2
Reputation: 26
descriptionTxt.Text = dt.Rows[0][1].ToString().Replace("Step1", "<b>Step1</b>");
Upvotes: -1