Introduction to Strings
Introduction to Strings
A String as a List
Overview
A string is an array or a collection of characters. A string is also a collection of strings, called sub-strings. This characteristic makes it possible to querry a string. Once again, the primary formula to query a string is:
var result-name = from result-holder in original-string select result-holder;
A String as a Collection of Characters
To perform selection operations on a string, you can declare a string variable. In this case, each character of the string would become a part of the result. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Exercice</title>
</head>
<body>
<h1>Exercise</h1>
@{
string country = "Equatorial Guinea";
var result = from character
in country
select character;
}
<ul>
@foreach(var c in result)
{
<li>@c</li>
}
</ul>
</body>
</html>
This would produce:
An Array of Strings for a Query
The classic way to query some strings is to start from an array of strings. In this case, you can first declare and define the array, then perform a query operation on it. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Department Store</title>
</head>
<body>
@{
string[] usernames = new string[] { "tmelone", "jblanton", "lmichaels", "dharrings", "dmathewson" };
var employees = from staff
in usernames
select staff;
}
<h2>Employees Usernames</h2>
<ul>
@foreach (var clerk in employees)
{
<li>@clerk</li>
}
</ul>
</body>
</html>
This would produce:
Splitting a String
If you have a long string of many characters or different words, you can segment it in different sections to get a collection of sub-strings. To support this, the String class is equipped with the Split() method. To get a collection of sub-strings from this method, you can call it on the in variable of your query. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Exercice</title>
</head>
<body>
<h1>Exercise</h1>
@{
string country = "United States of America";
var parts = from sub
in country.Split(new char[] { ' ' })
select sub;
}
<ul style="list-style-type: none">
@foreach(var c in parts)
{
<li>@c</li>
}
</ul>
</body>
</html>
This would produce:
Adding Selected Strings
The String class supports the + operator that can be used to add two strings and get a new string. Based on this, in your select statement, you can add strings as the result of your query. You can add a constant to each result. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Department Store</title>
</head>
<body>
@{
string[] usernames = new string[] { "tmelone", "jblanton", "lmichaels", "dharrings", "dmathewson" };
var employees = from staff
in usernames
select staff + "@departmentstore.com";
}
<h2>Employees Emails</h2>
<ul>
@foreach (var clerk in employees)
{
<li>@clerk</li>
}
</ul>
</body>
</html>
The String This would produce:
Concatenating Strings in a Select Statement
As an alternative to the + operator, the String class is equipped with the Concat() method that is overloaded with various versions. You can use any of those versions to add strings. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Department Store</title>
</head>
<body>
@{
string[] usernames = new string[] { "tmelone", "jblanton", "lmichaels", "dharrings", "dmathewson" };
var employees = from staff
in usernames
select string.Concat(staff, "@departmentstore.com");
}
<h2>Employees Emails</h2>
<ul>
@foreach (var clerk in employees)
{
<li>@clerk</li>
}
</ul>
</body>
</html>
Formatting a Selected String
As one more alternative, the String class provides the Format() method that can contain a placeholder for each string. Here is an example of calling it:
<!DOCTYPE html>
<html>
<head>
<title>Department Store</title>
</head>
<body>
@{
string[] usernames = new string[] { "tmelone", "jblanton", "lmichaels", "dharrings", "dmathewson" };
var employees = from staff
in usernames
select string.Format("{0}@departmentstore.com", staff);
}
<h2>Employees Emails</h2>
<ul>
@foreach (var clerk in employees)
{
<li>@clerk</li>
}
</ul>
</body>
</html>
String Interpolation
To complement this technique, the C# language provides the string interpolation that uses the $ symbol. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Department Store</title>
</head>
<body>
@{
string strAddress = "@departmentstore.com";
string[] usernames = new string[] { "tmelone", "jblanton", "lmichaels", "dharrings", "dmathewson" };
var employees = from staff
in usernames
select $"{staff}{strAddress}";
}
<h2>Employees Emails</h2>
<ul>
@foreach (var clerk in employees)
{
<li>@clerk</li>
}
</ul>
</body>
</html>
Comparisons in Querying a String
Introduction
The String class implements the IComparable interface. As a result, the class is equipped with various versions of the Compare() method. As a result, you can compare two strings for equality, and you can safely include the equality operator in a LINQ condition.
As seen so far, to perform comparisons in your LINQ statement, create a where condition. Either apply the necessary comparison operator or call the necessary method of the string data type.
Checking for String Equality
Like all classes of the .NET Framework, the String class inherits the Equals() method of the Object class. This makes it possible to compare two strings for equality or to compare a string to a constant. Here is an example of calling it:
<!DOCTYPE html>
<html>
<head>
<title>Geometry Inspired</title>
</head>
<body>
@{
string[] governmentShapes = new string[] { "Triangle", "Square", "Pentagon", "Hexagon", "Octagon",
"Department of Defense", "Pentagon", "France", "Hexagon" };
var shapes = from figure in governmentShapes
where figure.Equals("Pentagon")
select figure;
}
<h2>Geometry Inspired</h2>
<ul>
@foreach (var shape in shapes)
{
<li>@shape</li>
}
</ul>
</body>
</html>
If you want to find a string whose value is different from the condition, you have two options. You can use the != operator or you can negate the equality operation.
Getting the Items that Start with a Certain Sub-String
To let you get a list of strings that start with a certain letter or a sub-string, the String class is equipped with various methods. One option is to call the StartsWith() method. On the other hand, to get a list of string that don't start with a certain letter or sub-string, you can negate the call to that method. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>States Statistics</title>
</head>
<body>
@{
string[] entities = new string[] { "Montana", "Texas", "Commonwealth of Massachusetts", "Nebraska", "Hawaii",
"Commonwealth of Pennsylvania", "Arkansas", "Commonwealth of Virginia",
"Wisconsin", "Commonwealth of Kentucky", "Florida", "New Hampshire" };
var states = from gov in entities
where gov.StartsWith("Commonwealth")
select gov;
}
<h2>Commonwealth States</h2>
<ul>
@foreach (var name in states)
{
<li>@name</li>
}
</ul>
</body>
</html>
This would produce:
The String.StartsWith() method returns true if the variable that called it starts with the argument. You can negate the result to find the strings that don't start with the argument. To do this, apply the ! operation. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>States Statistics</title>
</head>
<body>
@{
string[] entities = new string[] { "Montana", "Texas", "Commonwealth of Massachusetts", "Nebraska", "Hawaii",
"Commonwealth of Pennsylvania", "Arkansas", "Commonwealth of Virginia",
"Wisconsin", "Commonwealth of Kentucky", "Florida", "New Hampshire" };
var states = from gov in entities
where !gov.StartsWith("Commonwealth")
select gov;
}
<h2>States List</h2>
<ul>
@foreach (var name in states)
{
<li>@name</li>
}
</ul>
</body>
</html>n.StartsWith("P")
This would produce:
To make the expression easy to read, you should include it in parentheses. Here is an example:
@{ string[] entities = new string[] { "Montana", "Texas", "Commonwealth of Massachusetts", "Nebraska", "Hawaii", "Commonwealth of Pennsylvania", "Arkansas", "Commonwealth of Virginia", "Wisconsin", "Commonwealth of Kentucky", "Florida", "New Hampshire" }; var states = from gov in entities where !(gov.StartsWith("Commonwealth")) select gov; }
Getting the Items that End with a Certain Sub-String
To let you get a list of strings that end with a certain letter or sub-string, the String class provides the EndsWith() method. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Chemistry</title>
</head>
<body>
@{
string[] elements = new string[] { "Hydrogen", "Helium", "Lithium", "Beryllium", "Boron", "Carbon",
"Nitrogen","Oxygen", "Fluorine", "Neon", "Sodium", "Magnesium" };
var atoms = from elm in elements
where elm.EndsWith("ium")
select elm;
}
<h2>Chemistry</h2>
<ul>
@foreach (var element in atoms)
{
<li>@element</li>
}
</ul>
</body>
</html>
This would produce:
To negate this operation, apply the ! operator to it.
Getting the Items that Contain a Certain Letter or Sub-String
To let yo get a select list of strings that contain a certain symbol or a certain sub-string, the String class provides the Contains() method. Here is an example of calling it:
<!DOCTYPE html>
<html>
<head>
<title>Actors/Actresses</title>
</head>
<body>
@{
string[] actors = new string[] { "Sylvester Stallone", "Robert De Niro", "Jean-Claude Van Damme",
"Rebecca De Mornay", "Rae Dawn Chong", "Ursula Andress",
"Gabrielle Union", "Danny DeVito", "Arnold Schwarzenegger" };
var celebrities = from act in actors
where act.Contains("De")
select act;
}
<h2>Actors/Actresses</h2>
<ul>
@foreach (var name in celebrities)
{
<li>@name</li>
}
</ul>
</body>
</html>
This would produce:
Notice the absence of Jean-Claude because of case-sensitivity.
To negate the expression, that is, to get the list of strings that don't contain a certain character or sub-string, precede the expression with !. Here is an example:
<!DOCTYPE html> <html> <head> <title>Actors/Actresses</title> </head> <body> @{ string[] actors = new string[] { "Sylvester Stallone", "Robert De Niro", "Jean-Claude Van Damme", "Rebecca De Mornay", "Rae Dawn Chong", "Ursula Andress", "Gabrielle Union", "Danny DeVito", "Arnold Schwarzenegger" }; var celebrities = from act in actors where !act.Contains("De") select act; } <h2>Actors/Actresses</h2> <ul> @foreach (var name in celebrities) { <li>@name</li> } </ul> </body> </html>
This would produce:
Arranging a String-Based Query
Sorting in Ascending Order
While you are querying a list of characters or string, you can arrange the result in alphabetical character. To do this, before the select operation, type the orderdy operator followed by the variable of the from clause. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Chemistry</title>
</head>
<body>
@{
string[] elements = new string[] { "Hydrogen", "Helium", "Lithium",
"Beryllium", "Boron", "Carbon" };
var atoms = from elm in elements
orderby elm
select elm;
}
<h2>Chemistry</h2>
<h3>Periodic Table</h3>
<ul>
@foreach (var element in atoms)
{
<li>@element</li>
}
</ul>
</body>
</html>
This would produce:
If you apply the orderby operator simply followed by a variable, the list is ordered alphabetically or numerically depending on the types of values in the list. This is referred to as ascending. To re-enforce this, you can follow the variable with the ascending keyword. Here is an example:
@{
string[] elements = new string[] { "Hydrogen", "Helium", "Lithium",
"Beryllium", "Boron", "Carbon" };
var atoms = from elm in elements
orderby elm ascending
select elm;
}
Sorting in Descending Order
You can arrange a list in reverse ascending order, in decremental order, or in reverse chronological order. To support this, the LINQ provides the orderby keyword in combination with the descending keyword. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Chemistry</title>
</head>
<body>
@{
string[] elements = new string[] { "Hydrogen", "Helium", "Lithium", "Beryllium",
"Boron", "Carbon", "Nitrogen", "Oxygen" };
var atoms = from elm in elements
orderby elm descending
select elm;
}
<h2>Chemistry</h2>
<ul>
@foreach (var element in atoms)
{
<li>@element</li>
}
</ul>
</body>
</html>
This would produce:
Conjunctions and Disjunctions
Disjunctions
If you are checking many conditions for a list you have, you many want only one of those conditions to be true for the combination to be true. The ability to combine various conditions so that only one of them needs to be true for the whole group to be true, is called a disjunction.
To support disjunctions, the LINQ provides the || operator. This is the same operator used in the C# language. To perform a disjunction, create a where condition, followed by (at least) two conditions. Separate the conditions with the || operator. The formula to follow is:
where condition_1 || condition_2
Each condition is created as a Boolean expression. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>Geometry Inspired</title>
</head>
<body>
@{
string[] governmentShapes = new string[] { "Triangle", "Square", "Pentagon", "Hexagon", "Octagon",
"Department of Defense", "Pentagon", "France", "Hexagon" };
var shapes = from figure in governmentShapes
where figure == "Pentagon" || figure == "Hexagon"
select figure;
}
<h2>Geometry Inspired</h2>
<ul>
@foreach (var shape in shapes)
{
<li>@shape</li>
}
</ul>
</body>
</html>
This would produce:
Once again, remember that it is a good idea to include the disjunction in parentheses to make it easy to read. Here is an example:
@{
string[] governmentShapes = new string[] { "Triangle", "Square", "Pentagon", "Hexagon", "Octagon",
"Department of Defense", "Pentagon", "France", "Hexagon" };
var shapes = from figure in governmentShapes
where ((figure == "Pentagon") || (figure == "Hexagon"))
select figure;
}
|
||
Previous | Copyright © 2008-2019, FunctionX, Inc. | Home |
|