Overview

This tutorial will teach coders how to handle pieces of Strings. You will learn how to get substrings, how to split strings into multiple pieces around a given token, and how to combine strings together.

Substrings

Sometimes, you are given a String and you need to get a substring of it. If you know the exact location within the string that you need the substring, you can easily obtain the substring. Let's first declare our strings.

String hi = "Hello World";

Now, suppose we want just the word "World" This is a suffix of our string, so we can do this easily. Just use the index of the first character of the substring when you call the following function:

hi.substring(6);

By adding a second argument to our function, we can get a substring that is in the middle of our string, like "lo Wo" In Java, the second argument is the start index plus the length. In C++, the second argument is the length of the substring.

hi.substring(3,8);

Tokenizing

When you don't know how long substrings that you need are, but you do know that each one is seperated by the same delimeter then you can use a function to tokenize your string about that delimeter. The Java split function takes a regular expression as input and outputs a string array of tokens. For example if you have a time that is split up as "HH:MM:SS" such as "01:30:00" then calling the function with the token ":" would produce {"01","30","00"}

String time = "01:30:00"; String parts[] = time.split(":");

Be careful with this though, a regular expression and a string aren't the same thing. Regular expressions are actually more powerful and there will be another tutorial on them in the future, but as a warning, split(".") doesn't work the way you'd expect.

Concatenation

Putting two strings together is a simple task in both languages. We're going to have two strings and we want a third string to be the concatenation of the first two.

String cole = "Cole is "; String adj = "awesome."; String sentence = cole.concat(adj);

Challenge

Program 1:
You will be given a string time and will need to return a string militaryTime.
time will be in the following format. HHMMX where HH is between 01 and 12 inclusive, MM is between 01 and 59 inclusive, and X is either A or P (for AM and PM respectively). Noon will be written as "1200P"
militaryTime should be of the form HHMM, where HH is between 00 and 23 inclusive, and MM is between 00 and 59 inclusive.

Sample Inputs:
"1200A"
"0801A"
"1159P"
"0330P"

Sample Outputs:
"0000"
"0801"
"2359"
"1530"

Program 2:
You will be given a string record which will have its fields seperated by colons, like so...
First Name : Last Name : Address
Address will be seperated into fields by commas, like so...
Street Address, City, State, Zip Code
You are to return a string shortRecord which will use some of the above strings with new seperators. Return in the following form:
Last Name, First Name : State

Sample Inputs:
"Scruff:McGruff:1 Crimestopper Ln.,Chicago,IL,60652"
"Stick:Stickley:P.O. Box 963, New York City,NY,10108"
"Oscar:Grouch:123 Sesame St.,Columbus,OH,12345"

Sample Outputs:
"McGruff,Scruff:IL"
"Stickley,Stick:NY"
"Grouch,Oscar:OH"

plants