Home > Articles > Programming > C#

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Change 2: RawHTML-->ImageList

With the raw HTML stored in a string, we can code the next information change—extracting the list of (.gif) files from the raw HTML:

string:URL-->string:RawHTML-->ArrayList:ImageList-->Disk:Files

Following the method-naming convention established in the previous section, we call our method RawHTMLtoImageList. This method takes one parameter—a string representing the raw HTML (raw_html)—and returns a list of files stored in an ArrayList, which we'll discuss shortly. Our method declaration is shown in Listing 27.

Listing 27—Method Declaration: RawHTMLtoImageList

public ArrayList RawHTMLtoImageList(string raw_html)
{
}

We code this method's implementation using two motifs.

Motif 3: Finding and Extracting a SubString

Recall that the transformation we're coding is as follows:

string:URL-->string:RawHTML-->ArrayList:ImageList-->Disk:Files

Whenever you want to extract a substring that's in some arbitrary location within a much larger string, you can use the motif in Listing 28:

Listing 28—Motif 3: RawHTML to One Substring

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);

In our motif above, the much larger string is labeled raw_html and the substring is labeled file. The following detailed explanation of this motif is based on the steps in the example we covered earlier in the section "Understanding the Vampire Bot." If you skipped the earlier example, you might want to go back and read it first.

Step 1—Finding a Close Pattern

We define a string patt and an integer ploc (see Listing 29):

Listing 29—Explanation Motif 3: Defining "Close" Pattern Variables

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);

Next, we set the patt to the value (".gif") that gets us close to the image (see Listing 30):

Listing 30—Explanation Motif 3: Setting the "Close" Pattern

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);

To find the pattern, we use the string object's built-in IndexOf() method. IndexOf() takes a string pattern and an integer representing the location from which to start searching, returning the position of the pattern as an integer. This is the general syntax of IndexOf():

int loc = ObjectName.IndexOf(string pattern, int start_search_location)

The code with this statement added looks as follows (see Listing 31):

Listing 31—Explanation Motif 3: IndexOf() to Find the "Close" Pattern

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);

Step 2: Finding the Starting Pattern

We define a string spat and an integer sloc (see Listing 32):

Listing 32—Explanation Motif 3: Defining "Starting" Pattern Variable

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);

Now that we have the location (ploc) of the pattern (patt) that gets us close to the image's filename, we need to search backward (starting at ploc) for the location of the starting pattern—a double quote. We set the starting pattern (spat) to this character, as shown in Listing 33.

Listing 33—Explanation Motif 3: Setting "Starting" Pattern

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);

NOTE

To put a double quote character between a pair of double quotes, you must preface the character with a backslash (\).

Every string object has a built-in method named LastIndexOf(), which takes both a string parameter representing a pattern to search for, and an integer denoting the position in the string to start searching for the pattern. LastIndexOf() then starts searching backward for the pattern at the specified location. This is the general syntax:

int loc=ObjectName.LastIndexOf(string pattern, int start_search_location)

The motif with this statement added looks as follows (see Listing 34):

Listing 34—Explanation Motif 3: Using LastIndexOf() to Find the "Starting" Pattern

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);

We add 1 to the value that LastIndexOf() returns, in order to position us at the actual start of the filename and not the starting pattern (the double quote).

Step 3: Finding the Ending Pattern

To find the ending pattern, we define a string epat and an integer eloc (see Listing 35).

Listing 35—Explanation Motif 3: Defining "Ending" Pattern Variable

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);

Now that we have the starting location (sloc) of the image's filename, we need to search forward (starting at sloc) for the location of the ending pattern—another double quote. We set the ending pattern (epat) to this character, as shown in Listing 36.

Listing 36—Explanation Motif 3: Setting "Ending" Patterns

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);

To search forward, we once again use the IndexOf() method as described above, to find the ending pattern, epat (see Listing 37):

Listing 37—Explanation Motif 3: Using IndexOf() to Find the "Ending" Pattern

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);

Note that we subtract 1 from eloc to position it at the actual end of the filename, and not at the ending pattern (a double quote).

Step 4: Extracting the Filename Substring

With the image filename's starting (sloc) and ending (eloc) locations known, we use the Substring() method to extract the filename into a new string. Substring() takes two parameters, an integer starting location and another integer denoting the number of characters to grab, returning the substring bounded by those parameters. The general syntax is shown below:

string subs=ObjectName.Substring(int starting_loc, int num_characters)

First we define a string named file (see Listing 38):

Listing 38—Explanation Motif 3: Defining Filename Variables

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);

Next, use the Substring() method to get the filename (see Listing 39):

Listing 39—Explanation Motif 3: Using Substring() to Extract the Image's Filename

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);

Note that the number of characters to grab, given a starting and ending location, is always: eloc-sloc+1.

For our vampire bot, we need to modify this motif slightly so that instead of just grabbing the filename of one image, it collects all the image filenames.

Motif 3': Finding and Extracting All SubStrings

To modify motif 3 so that it finds all the substrings in a string, instead of just the first substring, you simply loop through motif 3 until there are no substrings left (see Listing 40):

Listing 40—Modifying Motif 3 to Read All Substrings

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
while (ploc>=0) {
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);
ploc=raw_html.IndexOf  (patt, eloc);
}

IndexOf() returns a number less than 0 if it can't find a pattern (patt). Thus, in motif 3', the while loop's body is executed only if the pattern location (ploc) is greater than or equal to zero.

Note that the first call to IndexOf() started at the beginning of the string:

ploc=raw_html.IndexOf(patt,0)

The next call to IndexOf() should start looking for more patterns at the ending location (eloc) of the previous call, namely:

ploc=raw_html.IndexOf(patt,eloc)

By adding this line at the end of the loop, and moving the first call to IndexOf() prior to the loop, we generalize the motif to find and extract all the substrings we're interested in from a given string. The completed motif is shown in Listing 41.

Listing 41—Motif 3': RawHTML to All Substrings

string patt, spat, epat;
int  ploc, sloc, eloc;
string file;

patt=".gif";
spat="\"" ;
epat="\"" ;

ploc=raw_html.IndexOf  (patt, 0);
while (ploc>=0) {
sloc=raw_html.LastIndexOf(spat, ploc)+1;
eloc=raw_html.IndexOf  (epat, sloc)-1;
file=raw_html.Substring (sloc, eloc-sloc+1);
ploc=raw_html.IndexOf  (patt, eloc);
}

As is, the motif extracts all substrings but doesn't do anything with them—the variable holding the substring's value gets (file) overwritten every trip through the loop. The next motif we'll examine uses an object known as an ArrayList to store the values.

Motif 4: Storing Strings in an ArrayList

Whenever you have a number of items you need to store and you don't know a priori how many items you'll have, you should use an ArrayList object to store those items—particularly if later on in your code you want to easily loop over or randomly access the items. The motif is straightforward (see Listing 42):

Listing 42—Motif 4: Storing Substrings in an ArrayList

using System.Collections;

ArrayList list;
list = new ArrayList();
list.Add(file);

Detailed Explanation

You define an object of type ArrayList, which is part of the System.Collections namespace (see Listing 43):

Listing 43—Explanation Motif 4: Defining an ArrayList

using System.Collections;

ArrayList list;
list = new ArrayList();
list.Add(file);

Instantiate the object (see Listing 44):

Listing 44—Explanation Motif 4: Creating an ArrayList object

using System.Collections;

ArrayList list;
list = new ArrayList();
list.Add(file);

Then, whenever you want to add an item to the list, you use the ArrayList's built-in Add() method (see Listing 45):

Listing 45—Explanation Motif 4: Adding to an ArrayList

using System.Collections;

ArrayList list;
list = new ArrayList();
list.Add(file);

Next we combine these two motifs to implement the method.

Completing the Method: Combining Motifs

To complete the method, combine the motifs and return the ArrayList (list). Motif 3' is in green and motif 4 in aqua (Listing 46):

Listing 46—RawHTMLtoImageList, Completed Method: Combining Motifs 3' and 4

public ArrayList RawHTMLtoImageList(string raw_html)
{
string patt, spat, epat;
int  ploc, sloc, eloc;
string file;
ArrayList list;

patt=".gif";
spat="\"" ;
epat="\"" ;

list = new ArrayList();

ploc=raw_html.IndexOf  (patt, 0);
while (ploc>=0) {
 sloc=raw_html.LastIndexOf(spat, ploc)+1;
 eloc=raw_html.IndexOf  (epat, sloc)-1;
 file=raw_html.Substring (sloc, eloc-sloc+1);
ploc=raw_html.IndexOf  (patt, eloc);
list.Add(file);
}

return list;
}

The key to making the motifs work together is the placement of the Add() statement of motif 4' inside of the motif 3' while loop.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020