# Tutorial :: String Manipulation : Part 01 - Length( ), IndexOf( ) and Substring( )

Final Goal of the tutorial: The final goal of the tutorial is to take the fractional part of an entered string. Like the user entered "52.3035" and then we will cut out only ".3035".

Length( ) and IndexOf( ): To determine from where we will start cutting the string (In this case, from the position of the decimal point), we need the IndexOf() function. This function returns the index number of a given character or string. Before getting into deep, let first know the index numbers and positions.

Suppose that the following is a String

,

Dim myString As String = "I am Tanmay."

As I said in my first post about String ( that String is an array of characters, each of the characters of above given myString is having their index numbers as follows,

 I a m T a n m a y . 0 1 2 3 4 5 6 7 8 9 10 11

Thus, the index numbers starts from 0 and ends at a position (Length( ) - 1). The length( ) function returns an integer, which is the number of total number of characters in the string. For example,

Dim len As Integer = myString.Length()

Then the value of len will be 12. The important things to be noticed is that, In the case of character counting, the count begins from 1, but in the case of indexing, the number begins from 0.

Thats why I told you that the indexing ranges from 0 to (1 less than the length of the string). So in our case, the indexing is from 0 to (myString.Length()-1).

So, using the Length() function we can find the range of the indexes of any given string. It is required for working with the string, creating loops (The loop then can start from 0 and run till (Length()-1)).

Now, according to the goal of our tutorial, we supposed that the user will enter a floating point value and we will cut out the fractional part of the string, we thus need to find the index number of the decimal point from where we will start our cutting. Here we will need the IndexOf() function. This function also returns an integer which is the index number of our desired character or string. Thus taking,

Dim index_num As Integer = myString.IndexOf(".")

Please note that the IndexOf() function has 9 different forms with different parameters, here we are using the first form which is as,

IndexOf(value as char) as Inetger

Here, we will get, index_num = 11

if it was,

myString = "523.325"
index_num = myString.IndexOf(".")

We would get, index_num = 3

Whatever, now we have obtained the starting index for our cropping. Let us continue.

Substring( ): This function is used to crop-out a portion of the string. Like, "Tanmay" from "I am Tanmay". Substring function is of the form,

Form 01: Substring(startIndesx as Integer) As String
Form 02: Substring(startIndesx as Integer, length as Integer) As String

Here we have assumed that the entered number will be a floating point number, thus we will cut from the decimal point to the end of the string. Thus we can use the Form 01, where we will give the index from where the cutting will begin and will end at the end of string. Remember, that it returns a string, we will need a string to store the substring.

Dim mysubString as String = myString.Substring(index_num) 'here we have stored index number of the decimal point in index_num

Here if, myString was "52.035", then mysubString will become ".035" and our mission is done. Thus the final code is as follows,

Dim myString As String = "52.035"
Dim index_num As Integer = myString.IndexOf(".")
Dim mysubString As String = myString.Substring(index_num)

Now, let do a project with possible error handled.

We are using, VS.NET 2008 and creating the followings,

Form 01 : name = form1
Textbox 01 : name = mainString
Textbox 01 : name = subString
Button 01 : name = fractional_part

Now in the click event of the button fractional_part, we are writing the following code,

Dim myString As String = mainString.Text
Dim index_num As Integer = myString.IndexOf(".")
Dim mysubString As String = myString.Substring(index_num)

subString.Text = mysubString

In the above lines of codes, error may occur. To handle them, we can write the following codes.

Dim myString As String = mainString.Text
If myString <> "" Then      'ERROR_01
If myString.Contains(".") Then 'ERROR_02
Dim index_num As Integer = myString.IndexOf(".")
Dim mysubString As String = myString.Substring(index_num)
subString.Text = mysubString
Else
subString.Text = ".0"    'If the string is not having any decimal point, then we print out .0
End If
End If

Look, we can also write the following code for less code purpose,

subString.Text = mainString.text.Substring(mainString.text.IndexOf("."))

The errors that we have handled are, it may that the user did not entered anything but pressed the button (handled in ERROR_01), it may that the user did not use any decimal point (handled in ERROR_02). There can be several more errors too. We will discuss about them in the next part.

Remember, programming is all about exploring the possibilities.

## Author: Tanmay Chakrabarty

Tanmay Chakrabarty is a former CSE student, currently working as a Senior Software Engineer with 5+ years of experience in the field of Web Application development in PHP+MySQL platform with strong skills in Javascript, JQuery, JQuery UI and CSS. He tries to write notes every week but fails due to heavy loads of duty.