HTML Blox

Providing the building blox of the web.

  • HTML Blox Home
  • Blog
    • Design
    • Flash
    • General
    • HTML
    • Javascript
    • PHP
    • Programming
    • Web Servers
  • Forums
  • Where to Start?
  • Lessons
    • CSS
    • Design
    • HTML
    • Javascript
    • Programming
  • References
    • HTML
  • Resources
  • About
  • Contact
  • Go Green!

Programming Lessons

  1. Lesson 1: The Basics
  2. Lesson 2: A Brief History of Computers
  3. Lesson 3: The Workings of Computers
  4. Lesson 4: A Brief History of Programming
  5. Lesson 5: High-Level vs. Low-Level
  6. Lesson 6: Compiled vs. Interpreted
  7. Lesson 7: Variables
  8. Lesson 8.1: Arithmetic and Operators - Decimal
  9. Lesson 8.2: Arithmetic and Operators - Integer and Boolean
  10. Lesson 9.1: Conditional Statements - The Basics
  11. Lesson 9.2: Conditional Statements - Branching Conditionals
  12. Lesson 10.1: Loops - The Basics
  13. Lesson 10.2: Types of Loop
  14. Lesson 11: Functions
  15. Lesson 12: Strings
  16. Lesson 13: Procedural vs. Object-Oriented

Programming Reference

  1. No reference yet.

Links

  • W3C HTML 4.01 Strict Compliant
  • W3C CSS 2.1 Compliant
  • Web Standards Group Member
  • Follow us on Twitter
  • Number Overflow - Free Advanced Scripts and Tutorials
  • Azure Ronin Web Design - Professional Web Design and Development
  • XML Sitemap
  • U Comment, I Follow

Feeds

  • RSS 2.0 Feed
  • RDF/RSS 1.0 Feed
  • RSS 0.9 Feed
  • Atom Feed

Lesson 12: Strings

del.icio.us Digg Blinklist reddit

In our previous lesson we discussed functions. In this lesson, we’ll take a detailed look at strings and some related functions that are really common.

What are Strings?

You’ve seen strings in just about every previous lesson so far. A string is simply a “string” of character, generally represented between two double-quotes (“”). Strings are used to hold most non-constant/non-static text in programs. There are sometimes other data types to hold larger chunks of text, but often times they are just a type of string.

In nearly every language, strings have many built-in functions which provide you various actions that allow you to do various things. We’ll take a look at a few of the more useful and common ones.

Length

The Length function (also called count, strlen, or a property in an object-oriented language). This function simply returns the number of characters that is in the string. For example, if the string is “bob”, Length would return 3. If the string was “this is a duck”, you would get 14 (remember, spaces are also characters).

Substring

The Substring function (often shortened to substr), is a function that takes a string and will return a part of a string based on a starting index that you provide and a length.

An index is a zero-based number that specifies the position of a specific character (or element in an Array). So, for example, in the string “abcdef”, a has an index of 0, d has an index of 3, etc.

The Substring function uses these values to return parts of a string. The specifics of Substring vary from language to language, but most will take an index value that is less than the value of returned by Length, and a positive value for a length. Some languages will also allow you to pass a negative value for the length as well.

If you have a positive value for the length, it will start at the starting index and get the number of character equal to length, or as many as it can before it runs out of characters. If you have a negative value, it will still start at the same position, but it will cut that many characters off the end of the string.

For our concerns, we’ll say the syntax for Substring is:

function String Substring(String str, Integer start, Integer length)

Here are some examples:

Substring("abcdef",0,6) // "abcdef"
Substring("abcdef",2,2) // "cd"
Substring("abcdef",4,-1) // "de"
Substring("abcdef",0,-2) // "abcd"

IndexOf

IndexOf will return the index of the first instance of a specified character. As usual, the specifics very a lot, but in most languages if the character (or characters) you’re looking for in the string aren’t there, it’ll return -1. Also, some languages you can only have one character to search for, while others will take any number of characters (as a string).

We’ll say that our syntax of IndexOf is:

function Integer IndexOf(String str, Integer index)

And some examples:

IndexOf("abcdef","a") // 0
IndexOf("abcdef","b") // 1
IndexOf("abcdef","e") // 4
IndexOf("abcdef",z") // -1

Other Functions

Length, Substring, IndexOf are pretty universal throughout all programming languages. There are usually many many more in most languages.

Concat

Some languages require the use of a Concat (concatenation) function to put two strings together, instead of just adding them together like other languages.

Lowercase

Will take a string and return a string with all of the characters lowercase.

Uppercase

Will take a string and return a string with all of the characters uppercase.

Split

Will take a string and a “delimiter” (a character used as a seperator, such as a comma) and will break the string up into an array at those delimited points.

Example syntax:

function Array Split(String str, String del)

Examples:

Split("a,b,c",",") // {"a","b","c"}
Split("a|b|cd|efg|hi","|") // {"a","b","cd","efg","hi"}
Split("Bob is crazy and insane"," ") // {"Bob","is","crazy","and","insane"}


In this lesson we took a look at some very useful string functions that are used very commonly in practical applications. In our next lesson, we'll take a look at the difference between object-oriented and procedural programming.

This entry was posted on Thursday, November 12th, 2009 at 3:16 pm and is filed under Programming Lessons. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Please Login or Register or fill out the following to leave a reply:

If you haven't posted an approved comment before, your comment will not show up until approved.

  • HTML Blox Home
  • Blog
  • Design Blog
  • Flash Blog
  • General Blog
  • HTML Blog
  • Javascript Blog
  • PHP Blog
  • Programming Blog
  • Web Servers Blog
  • Forums
  • Where to Start?
  • Lessons
  • CSS Lessons
  • Design Lessons
  • HTML Lessons
  • Javascript Lessons
  • Programming Lessons
  • References
  • HTMLReferences
  • About
  • Resources
  • Contact
  • Go Green!
  • Sitemap

Copyright © 2008 HTML Blox

Unless otherwise noted, all images created and copyright HTML Blox.

Operated by Azure Ronin Web Design