Introduction to Strings
Introduction to Strings
The Nullity of a String
A Null String
A string is referred to as null if it doesn't (yet) have a(ny) character(s)s. This is usually the case when the string has just been created, or a string variable has just been declared. When you have just declared a variable, to indicate that it is null, assign the null keyword to it. The variable must be declared using the string data type, the String class, or the dynamic keyword. Here are example:
@{
string firstName = null;
dynamic lastName = null;
String homeAddress = null;
}
If you decide to use the var keyword to declare the variable, you cannot assign null to it.
A Null or Empty String
A string is referred to as null if it has lost its characters. To let you find out whether a string is empty or null, the String class is equipped with a static method named IsNullOrEmpty. Its syntax is:
public static bool IsNullOrEmpty(string value)
Practical Learning: Using a Null String
@{ ViewBag.Title = "Customer Invoice Preparation"; } <h2 class="text-center">Gas Utility Company</h2> <h3 class="text-center">Customer Bill Preparation</h3> <div class="horizontal-line"></div> @{ double CCFTotal = 0; double amountDue = 0; double localTaxes = 0; double stateTaxes = 0; double totalTherms = 0; double over50Therms = 0; double first50Therms = 0; double deliveryTotal = 0; double meterReadingEnd = 0; double meterReadingStart = 0; int invoiceNumber = 100001; string strErrorMessage = null; double environmentalCharges = 0; double transportationCharges = 0; double distributionAdjustment = 0; if (IsPost) { try { if (string.IsNullOrEmpty(Request["txtMeterReadingStart"].ToString())) { throw new FormatException("Meter Start Error"); } meterReadingStart = double.Parse(Request["txtMeterReadingStart"]); if (string.IsNullOrEmpty(Request["txtMeterReadingEnd"].ToString())) { throw new FormatException("Meter End Error"); } meterReadingEnd = double.Parse(Request["txtMeterReadingEnd"]); CCFTotal = meterReadingEnd - meterReadingStart; totalTherms = CCFTotal * 1.0367; distributionAdjustment = totalTherms * 0.13086; if (totalTherms <= 5000) { transportationCharges = totalTherms * 0.016289; } else { transportationCharges = totalTherms * 0.009577; } if (totalTherms < 5000) { first50Therms = totalTherms * 0.02269; over50Therms = 0; } else { first50Therms = 5000 * 0.02269; over50Therms = (totalTherms - 5000) * 0.04995; } deliveryTotal = transportationCharges + distributionAdjustment + first50Therms + over50Therms; environmentalCharges = deliveryTotal * 0.0045; localTaxes = (deliveryTotal + environmentalCharges) * 0.04862; stateTaxes = (deliveryTotal + environmentalCharges) * 0.020943; amountDue = deliveryTotal + environmentalCharges + localTaxes + stateTaxes; } catch (FormatException fe) { if (fe.Message == "Meter Start Error") { strErrorMessage = "Either you left the meter start text box empty or you entered an invalid value."; } if (fe.Message == "Meter End Error") { strErrorMessage = "The value provided for the counter end reading is not valid."; } } } } @using (Html.BeginForm()) { <table> <tr> <td class="left-column">Invoice #:</td> <td class="left-text-boxes">@Html.TextBox("txtInvoiceNumber", @invoiceNumber, new { @class = "txtContext" })</td> <td class="right-label"> </td> <td> </td> </tr> <tr> <td>Counter Reading Start:</td> <td>@Html.TextBox("txtMeterReadingStart", @meterReadingStart, new { @class = "txtContext" })</td> <td>Counter Reading End:</td> <td>@Html.TextBox("txtMeterReadingEnd", @meterReadingEnd, new { @class = "txtContext" })</td> </tr> </table> <p class="text-center"><input type="submit" class="btnLarge" value="Prepare Invoice" name="btnPrepareInvoice" /></p> <div class="horizontal-line"></div> <table> <tr> <td class="left-column">CCF Total:</td> <td class="left-text-boxes">@Html.TextBox("txtCCFTotal", @CCFTotal.ToString("F"), new { @class = "txtContext" })</td> <td class="right-label">Total Therms:</td> <td>@Html.TextBox("txtTotalTherms", @totalTherms.ToString("F"), new { @class = "txtContext" })</td> </tr> <tr> <td>Transportation Charges:</td> <td>@Html.TextBox("txtTransportationCharges", @transportationCharges.ToString("F"), new { @class = "txtContext" })</td> <td>Delivery Total:</td> <td>@Html.TextBox("txtDeliveryTotal", @deliveryTotal.ToString("F"), new { @class = "txtContext" })</td> </tr> </table> <div class="horizontal-line"></div> <table> <tr> <td class="left-column">Ditribution Adjust (*0.13086):</td> <td class="left-text-boxes">@Html.TextBox("txtDitributionAdjustment", @distributionAdjustment.ToString("F"), new { @class = "txtContext" })</td> <td class="right-label">Environment Charges:</td> <td>@Html.TextBox("txtEnvironmentCharges", @environmentalCharges.ToString("F"), new { @class = "txtContext" })</td> </tr> <tr> <td>First 50 Therms (*0.5269):</td> <td>@Html.TextBox("txtFirst50Therms", @first50Therms.ToString("F"), new { @class = "txtContext" })</td> <td>Local/County Taxess:</td> <td>@Html.TextBox("txtLocalTaxes", @localTaxes.ToString("F"), new { @class = "txtContext" })</td> </tr> <tr> <td>Over 50 Therms (*0.4995):</td> <td>@Html.TextBox("txtOver50Therms", @over50Therms.ToString("F"), new { @class = "txtContext" })</td> <td>State Taxess:</td> <td>@Html.TextBox("txtStateTaxes", @stateTaxes.ToString("F"), new { @class = "txtContext" })</td> </tr> </table> <div class="horizontal-line"></div> <table> <tr> <td class="left-column"> </td> <td class="left-text-boxes"> </td> <td class="right-label">Amount Due:</td> <td>@Html.TextBox("txtAmountDue", @amountDue.ToString("F"), new { @class = "txtContext" })</td> </tr> </table> <div class="horizontal-line"></div> <p>@strErrorMessage</p> }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace CountriesStatistics04.Controllers { public class GermanyController : Controller { // GET: Germany public ActionResult Index() { return View(); } // GET: GermanStates public ActionResult GermanStates() { return View(); } // GET: StateSearch public ActionResult StateSearch() { return View(); } } }
@{ ViewBag.Title = "State Search"; } <h2>Germany: State Search</h2> @{ string[] abbreviations = new string[] { "TH", "SL", "NW", "HH", "RP", "NI", "SH", "BB", "HE", "BE", "ST", "BW", "BY", "SN", "HB", "MV" }; string[] states = new string[] { "Saarland", "North Rhine-Westphalia", "Thuringia", "Hamburg", "Rhineland-Palatinate", "Lower Saxony", "Schleswig-Holstein", "Brandenburg", "Hesse", "Berlin", "Saxony-Anhalt", "Baden-Württemberg", "Bavaria", "Saxony", "Bremen", "Mecklenburg-Vorpommern" }; int[] areas = new int[] { 16172, 2569, 34085, 755, 19853, 47609, 15799, 29479, 21115, 892, 20446, 35752, 70552, 18416, 419, 23180 }; string[] capitals = new string[] { "Saarbrücken", "Düsseldorf", "Erfurt", "", "Mainz","Hanover", "Kiel", "", "Wiesbaden", "Stuttgart", "Magdeburg", "Munich", "Potsdam", "Dresden", "Bremen", "Schwerin" }; } @{ int iArea = 0; bool found = false; string strErrorMessage = ""; string strCapital = string.Empty; string strStateName = string.Empty; string strAbbreviation = string.Empty; string strStateLetters = string.Empty; if (IsPost) { } } @using (Html.BeginForm()) { <table style="width: 320px;"> <tr> <td class="left-col">State Letters:</td> <td>@Html.TextBox("txtStateLetters", "", new { style = "width: 60px" }) <input type="submit" name="btnFind" value="Find" class="left-col" /></td> </tr> <tr> <td>State:</td> <td>@Html.TextBox("txtAbbreviation", @strAbbreviation, new { style = "width: 40px" }) @Html.TextBox("txtStateName", @strStateName)</td> </tr> <tr> <td>Area:</td> <td>@Html.TextBox("txtArea", @iArea, new { @class = "medium-text" }) Km<sup>2</sup></td> </tr> <tr> <td>Capital:</td> <td>@Html.TextBox("txtCapital", @strCapital)</td> </tr> </table> <p style="color: red">@strErrorMessage</p> }
A String With a White Space
A string contains a white space if it was previously initialized with at least one character and all of its characters have been removed or the string was simply initialized with something like the Space bar of the keyboard. Here is an example:
string whiteSpace = " ";
A Null, Empty, or White-Spaced String
To let you find out whether a string is null or contains a white space, the String class is equipped with a static method named IsNullOrWhiteSpace. Its syntax is:
public static bool IsNullOrWhiteSpace(string value)
Practical Learning: Checking the Nullity of a String
@{
ViewBag.Title = "State Search";
}
<h2>State Search</h2>
@{
. . . No Change
}
@{
int iArea = 0;
bool found = false;
string strErrorMessage = "";
string strCapital = string.Empty;
string strStateName = string.Empty;
string strAbbreviation = string.Empty;
string strStateLetters = string.Empty;
if (IsPost)
{
if (string.IsNullOrWhiteSpace(Request["txtStateLetters"].ToString()))
{
strErrorMessage = "You must type a 2-letter abbreviation for a state";
}
}
}
@using (Html.BeginForm())
{
. . . No Change
}
Strings Comparisons
String Equality
The indexed-equivalent characters of two strings can be compared to know whether one is lower or higher than the other's. If you are only interested to know whether two strings are equivalent, to assist you with that operation, the string class is equipped with a method named Equals. It is overloaded with various versions. Two versions use the following syntaxes:
public override bool Equals(object obj); public bool Equals(string value);
When calling one of these versions, use an object object or a string value that calls it. The method takes one argument. The variable that calls the method is compared to the value passed as argument. If both values are the exact same, the method returns true. The comparison is performed considering the case of each character. If you don't want to consider the case, use the following version of the method:
public bool Equals(string value, StringComparison comparisonType);
An alternative to the second syntax is to use a static version of this method whose syntax is:
public static bool Equals(string a, string b);
This method takes two string arguments and compares them. If they are the same, the method returns true. This method considers the cases of the characters. If you don't want this factor taken into consideration, use the following version of the method:
public static bool Equals(string a, string b, StringComparison comparisonType);
Practical Learning: Comparing Two Strings for Equality
@{
ViewBag.Title = "State Search";
}
<h2>State Search</h2>
@{
string[] abbreviations = new string[]
{
"TH", "SL", "NW", "HH", "RP", "NI", "SH", "BB", "HE", "BE", "ST", "BW", "BY", "SN", "HB", "MV"
};
string[] states = new string[]
{
"Saarland", "North Rhine-Westphalia", "Thuringia", "Hamburg", "Rhineland-Palatinate", "Lower Saxony", "Schleswig-Holstein", "Brandenburg", "Hesse", "Berlin", "Saxony-Anhalt", "Baden-Württemberg", "Bavaria", "Saxony", "Bremen", "Mecklenburg-Vorpommern"
};
int[] areas = new int[]
{
16172, 2569, 34085, 755, 19853, 47609, 15799, 29479, 21115, 892, 20446, 35752, 70552, 18416, 419, 23180
};
string[] capitals = new string[]
{
"Saarbrücken", "Düsseldorf", "Erfurt", "", "Mainz","Hanover", "Kiel", "", "Wiesbaden", "Stuttgart", "Magdeburg", "Munich", "Potsdam", "Dresden", "Bremen", "Schwerin"
};
}
@{
int iArea = 0;
bool found = false;
string strErrorMessage = "";
string strCapital = string.Empty;
string strStateName = string.Empty;
string strAbbreviation = string.Empty;
string strStateLetters = string.Empty;
if (IsPost)
{
if (string.IsNullOrWhiteSpace(Request["txtStateLetters"].ToString()))
{
strErrorMessage = "You must type a 2-letter abbreviation for a state";
}
else
{
strStateLetters = Request["txtStateLetters"].ToString();
for (int i = 0; i < 16; i++)
{
if (strStateLetters.Equals(abbreviations[i]))
{
iArea = areas[i];
strStateName = states[i];
strCapital = capitals[i];
strAbbreviation = abbreviations[i];
found = true;
break;
}
}
}
if(found == false)
{
strErrorMessage = "There is no state with those letters.";
}
}
}
@using (Html.BeginForm())
{
<table style="width: 320px;">
<tr>
<td class="left-col">State Letters:</td>
<td>@Html.TextBox("txtStateLetters", "", new { style = "width: 60px" })
<input type="submit" name="btnFind" value="Find" class="left-col" /></td>
</tr>
<tr>
<td>State:</td>
<td>@Html.TextBox("txtAbbreviation", @strAbbreviation, new { style = "width: 40px" })
@Html.TextBox("txtStateName", @strStateName)</td>
</tr>
<tr>
<td>Area:</td>
<td>@Html.TextBox("txtArea", @iArea, new { @class = "medium-text" }) Km<sup>2</sup></td>
</tr>
<tr>
<td>Capital:</td>
<td>@Html.TextBox("txtCapital", @strCapital)</td>
</tr>
</table>
<p>@strErrorMessage</p>
}
String Integral Comparison
String comparison consists of examining the characters of two strings with a character of one string compared to a character of the other string with both characters at the same positions. To support this operation, the string class is equipped with the Compare() method that is overloaded with many versions. One of the versions uses the following syntax:
public static int Compare(string String1, string String2);
This method is declared static and it takes two arguments. The method returns
Here is an example:
@{ string firstName1 = "Andy"; string lastName1 = "Stanley"; string firstName2 = "Charles"; string lastName2 = "Stanley"; int Value1 = string.Compare(firstName1, firstName2); int Value2 = string.Compare(firstName2, firstName1); int Value3 = string.Compare(lastName1, lastName2); }
When using this version of the string.Compare() method, the case (upper or lower) of each character is considered. If you don't want to consider this factor, the String class proposes another version of the method. Its syntax is:
public static int Compare(string String1, string String2, bool ignoreCase);
The third argument allows you to ignore the case of the characters when performing the comparison.
Practical Learning: Integrally Comparing Strings
@{
ViewBag.Title = "Employee Account Creation";
}
<h2>Employee Account Creation</h2>
<div class="horizontal-line"></div>
@{
int length = 0;
int digits = 0;
int symbols = 0;
int lowercase = 0;
int uppercase = 0;
string strUsername = string.Empty;
string strLastName = string.Empty;
string strFirstName = string.Empty;
string strMiddleName = string.Empty;
string strNewPassword = string.Empty;
string strErrorMessage = string.Empty;
string strConfirmPassword = string.Empty;
if (IsPost)
{
strNewPassword = Request["txtNewPassword"].ToString();
length = strNewPassword.Length;
strConfirmPassword = Request["txtConfirmPassword"].ToString();
for (int i = 0; i < length; i++)
{
if (Char.IsDigit(strNewPassword[i]))
{
digits++;
}
if (Char.IsSymbol(strNewPassword[i]) || Char.IsPunctuation(strNewPassword[i]))
{
symbols++;
}
if (Char.IsLower(strNewPassword[i]))
{
lowercase++;
}
if (Char.IsUpper(strNewPassword[i]))
{
uppercase++;
}
}
if (string.Compare(strNewPassword, strConfirmPassword) == 0)
{
strErrorMessage = "Your password will be saved successfully.";
}
else
{
strErrorMessage = "The passwords don't match.";
}
}
}
@using (Html.BeginForm())
{
<table>
<tr>
<td class="left-col">First Name:</td>
<td>@Html.TextBox("txtFirstName", @strFirstName)</td>
</tr>
<tr>
<td>Middle Name:</td>
<td>@Html.TextBox("txtMiddleName", @strMiddleName)</td>
</tr>
<tr>
<td>Last Name:</td>
<td>@Html.TextBox("txtLastName", @strLastName)</td>
</tr>
<tr>
<td>Username:</td>
<td>@strUsername</td>
</tr>
</table>
<div class="horizontal-line"></div>
<table>
<tr>
<td class="left-col">New Password:</td>
<td>@Html.TextBox("txtNewPassword", @strNewPassword)</td>
<td>@Html.TextBox("txtCharacters", @length, new { @class = "small-text" }) Characters</td>
</tr>
<tr>
<td>Confirm Password:</td>
<td>@Html.TextBox("txtConfirmPassword", @strConfirmPassword)</td>
<td> </td>
</tr>
</table>
<table>
<tr>
<td class="left-col"> </td>
<td class="large">@Html.TextBox("txtLowercase", @lowercase, new { @class = "small-text" }) Lowercase Letters</td>
<td>@Html.TextBox("txtUppercase", @uppercase, new { @class = "small-text" }) Uppercase Letters</td>
</tr>
<tr>
<td class="left-col"> </td>
<td>@Html.TextBox("txtDigits", @digits, new { @class = "small-text" }) Digits</td>
<td>@Html.TextBox("txtSymbols", @symbols, new { @class = "small-text" }) Symbols</td>
</tr>
</table>
<div class="horizontal-line"></div>
<p class="text-center"><input type="submit" name="btnSubmit" value="Submit" class="btnLarge" /></p>
<div class="horizontal-line"></div>
<p>@strErrorMessage</p>
}
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Chemistr09.App_Code { public enum Phase { Gas, Liquid, Solid, Unknown } public class Element { public string Symbol { get; set; } = ""; public string ElementName { get; set; } = ""; public int AtomicNumber { get; set; } = 0; public float AtomicWeight { get; set; } = 0.000F; public Phase Phase { get; set; } = Phase.Unknown; public Element() { } public Element(int number) { AtomicNumber = number; } public Element(string symbol) { Symbol = symbol; } public Element(int number, string symbol, string name, float mass) { Symbol = symbol; ElementName = name; AtomicWeight = mass; AtomicNumber = number; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Chemistry10.Controllers { public class HomeController : Controller { // GET: Home public ActionResult Index() { return View(); } public ActionResult About() { ViewBag.Message = "Your application description page."; return View(); } public ActionResult Contact() { ViewBag.Message = "Your contact page."; return View(); } // GET: ElementSearch public ActionResult ElementSearch() { return View(); } // GET: Compounds public ActionResult Compounds() { return View(); } // GET: PeriodicTable public ActionResult PeriodicTable() { return View(); } } }
@{ ViewBag.Title ="Chemical Compounds"; } <h2 class="text-center">Chemical Compounds</h2>
@{ ViewBag.Title = "Periodic Table"; } <h2 class="text-center">Periodic Table</h2>
String-Case Conversions
Converting a String to Lowercase
To let you convert a string from lowercase to uppercase, the String class is equipped with a method named ToLower. It is overloaded with two versions. One of the versions of this method uses the following syntax:
public string ToLower()
This method considers each character of the string that called it. If the character is already in uppercase, it would not change. If the character is a lowercase alphabetic character, it would be converted to uppercase. If the character is not an alphabetic character, it would be kept "as-is".
Practical Learning: Converting a String to Lowercase
@{ ViewBag.Title = "Element Search"; } <h2>@ViewBag.SubTitle</h2> @{ Chemistry10.App_Code.Element selected = new Chemistry10.App_Code.Element(); Chemistry10.App_Code.Element H = new Chemistry10.App_Code.Element(1, "H", "Hydrogen", 1.008F) { Phase = Chemistry10.App_Code.Phase.Gas }; Chemistry10.App_Code.Element He = new Chemistry10.App_Code.Element(2, "He", "Helium", 4.002602F) { Phase = Chemistry10.App_Code.Phase.Gas }; Chemistry10.App_Code.Element Li = new Chemistry10.App_Code.Element(3, "Li", "Lithium", 6.94F) { Phase = Chemistry10.App_Code.Phase.Solid }; Chemistry10.App_Code.Element Be = new Chemistry10.App_Code.Element(4, "Be", "Beryllium", 9.0121831F) { Phase = Chemistry10.App_Code.Phase.Solid }; Chemistry10.App_Code.Element B = new Chemistry10.App_Code.Element(5, "B", "Boron", 10.81F) { Phase = Chemistry10.App_Code.Phase.Solid }; Chemistry10.App_Code.Element C = new Chemistry10.App_Code.Element(name: "Carbon", mass: 12.011F, symbol: "C", number: 6) { Phase = Chemistry10.App_Code.Phase.Solid }; Chemistry10.App_Code.Element N = new Chemistry10.App_Code.Element(7, "N", "Nitrogen", 14.007F) { Phase = Chemistry10.App_Code.Phase.Gas }; Chemistry10.App_Code.Element O = new Chemistry10.App_Code.Element(8, "O", "Oxygen", 15.999F) { Phase = Chemistry10.App_Code.Phase.Gas}; Chemistry10.App_Code.Element F = new Chemistry10.App_Code.Element(9, "F", "Fluorine", 15.999F) { Phase = Chemistry10.App_Code.Phase.Gas }; Chemistry10.App_Code.Element Ne = new Chemistry10.App_Code.Element("Ne") { AtomicNumber = 10, ElementName = "Neon", AtomicWeight = 20.1797F, Phase = Chemistry10.App_Code.Phase.Gas }; Chemistry10.App_Code.Element Na = new Chemistry10.App_Code.Element(11, "Na", "Sodium", 22.98976928F) { Phase = Chemistry10.App_Code.Phase.Solid }; Chemistry10.App_Code.Element Mg = new Chemistry10.App_Code.Element(12, "Mg", "Magnesium", 24.305F) { Phase = Chemistry10.App_Code.Phase.Solid }; Chemistry10.App_Code.Element Al = new Chemistry10.App_Code.Element(13, "Al", "Aluminium", 26.9815385F) { Phase = Chemistry10.App_Code.Phase.Solid }; Chemistry10.App_Code.Element Si = new Chemistry10.App_Code.Element() { ElementName = "Silicon", AtomicWeight = 28.085F, Symbol = "Si", AtomicNumber = 14, Phase = Chemistry10.App_Code.Phase.Solid }; Chemistry10.App_Code.Element P = new Chemistry10.App_Code.Element() { ElementName = "Phosphorus", AtomicWeight = 30.973761998F, Symbol = "P", AtomicNumber = 15, Phase = Chemistry10.App_Code.Phase.Solid }; Chemistry10.App_Code.Element S = new Chemistry10.App_Code.Element(16, "S", "Sulfur", 32.06F) { Phase = Chemistry10.App_Code.Phase.Solid }; Chemistry10.App_Code.Element Cl = new Chemistry10.App_Code.Element(17, "Cl", "Chlorine", 35.45F) { Phase = Chemistry10.App_Code.Phase.Gas }; Chemistry10.App_Code.Element Ar = new Chemistry10.App_Code.Element(18, "Ar", "Potassium", 39.098F) { Phase = Chemistry10.App_Code.Phase.Solid }; string strSymbolEntered = ""; if (IsPost) { strSymbolEntered = Request["txtSymbolEntered"].ToLower(); switch (strSymbolEntered) { case "h": selected = H; break; case "he": selected = He; break; case "li": selected = Li; break; case "be": selected = Be; break; case "b": selected = B; break; case "c": selected = C; break; case "n": selected = N; break; case "o": selected = O; break; case "f": selected = F; break; case "ne": selected = Ne; break; case "na": selected = Na; break; case "mg": selected = Mg; break; case "al": selected = Al; break; case "si": selected = Si; break; case "p": selected = P; break; case "s": selected = S; break; case "cl": selected = Cl; break; case "ar": selected = Ar; break; } ViewBag.SubTitle = selected.ElementName; } } <div class="sub-title-holder"> <div id="sub-title">@ViewBag.SubTitle</div> <hr /> </div> <div class="elm-presentation"> @using (Html.BeginForm()) { <table> <tr> <td class="emphasize">Enter symbol:</td> <td> @Html.TextBox("txtSymbolEntered", @strSymbolEntered, new { @class = "small-text" }) <input type="submit" name="btnFind" value="Find" style="width: 70px;" /> </td> </tr> <tr> <td class="emphasize">Symbol:</td> <td>@Html.TextBox("txtSymbol", @selected.Symbol)</td> </tr> <tr> <td class="emphasize">Element Name:</td> <td>@Html.TextBox("txtElementName", @selected.ElementName)</td> </tr> <tr> <td class="emphasize">Atomic Weight:</td> <td>@Html.TextBox("txtAtomicWeight", @selected.AtomicWeight)</td> </tr> <tr> <td class="emphasize">Phase:</td> <td>@Html.TextBox("txtAtomicWeight", @selected.Phase)</td> </tr> </table> } </div>
Converting a String to Uppercase
To let you convert a string to uppercase, the String class is equipped with a method named ToUpper. This method is overloaded with two versions. The syntax of one of the versions is:
public string ToUpper()
Here is an example:
@{ string strFullName = "Alexander Patrick Katts"; string strConversion = strFullName.ToUpper(); }
Practical Learning: Converting a String to Uppercase
@{
ViewBag.Title = "State Search";
}
<h2>Germany: State Search</h2>
@{
. . . No Change
}
@{
int iArea = 0;
bool found = false;
string strErrorMessage = "";
string strCapital = string.Empty;
string strStateName = string.Empty;
string strAbbreviation = string.Empty;
string strStateLetters = string.Empty;
if (IsPost)
{
if (string.IsNullOrWhiteSpace(Request["txtStateLetters"].ToString()))
{
strErrorMessage = "You must type a 2-letter abbreviation for a state";
}
else
{
strStateLetters = Request["txtStateLetters"].ToString().ToUpper();
for (int i = 0; i < 16; i++)
{
if (strStateLetters.Equals(abbreviations[i]))
{
iArea = areas[i];
strStateName = states[i];
strCapital = capitals[i];
strAbbreviation = abbreviations[i];
found = true;
break;
}
}
}
if (found == false)
{
strErrorMessage = "There is no state with those letters.";
}
}
}
@using (Html.BeginForm())
{
. . . No Change
}
|
||
Previous | Copyright © 2001-2019, FunctionX | Next |
|