0

I have a string which is a paragraph written on the aspx side. It goes like this:

The new student, {student_name} has the following grades -
Maths - {math_grade}
Science - {Science_grade}
...
and so on.

I need to get values from database, and replace {student_name} with Joe Smith, {Math_grade} wth A or B+ etc.

How can I do this?

4 Answers 4

5

String.Replace

Sign up to request clarification or add additional context in comments.

Comments

4
Dim myString As String = "{student_name}"

myString = myString.Replace("{student_name}","Joe Smith")

...Remember to always assign the string you are attempting to replace values in to the original string.

myString.Replace("value","newvalue")

...will not replace your original strings values -- it must be explicitly assigned. e.g.

myString = myString.Replace("value","newvalue")

Comments

0

You certainly can use String.Replace, but typically ASP.NET apps are written using labels as placeholders and then modifying the values in the code-behind.

So your MyFile.aspx would contain The new student, [lblStudentName] has the following grades - Maths - [lblMathGrade] Science - [lblScienceGrade] ... and so on

Then in your MyFile.aspx.cs (or MyFile.aspx.vb)

this.lblStudentName.Text = "John Doe";
this.lblMathGrade.Text = "A-";
this.lblScienceGrade.Text = "F";

There are also many other better techniques you could use, such as a repeater control, then binding the sql results to this control.

Comments

0

String interpolation was introduced in .NET Framework 4.6 which can be a lot cleaner way of doing things. You prefix the string with $ sign.

Dim studentName As String = "Joe Smith"
Dim myString As String = $"Hello, {studentName}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.