BACS 287 Visual Basic String Manipulation. BACS 287 Visual Basic Strings In Visual Basic, a “string” is a series of text, numbers, and special characters.

Slides:



Advertisements
Similar presentations
Manipulating Strings String Functions. VB provides a large number of functions that facilitate working with strings. These are found in Microsoft.VisualBasic.Strings.
Advertisements

Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
MIS: Chapter 14 Cumulative concepts, features and functions, plus new functions COUNTIFS, SUMIFS, AVERAGEIFS (Separate ppt on REACH.louisville.edu) All.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Slide 1 VB Programming Fundamentals. Slide 2 Visual Basic Language v VB language is powerful and easy to use v Descendent of BASIC (Beginner's All-Purpose.
1 Pertemuan 04 Expression Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Strings in Visual Basic Words, Phrases, and Spaces.
Introduction to Oracle9i: SQL1 Selected Single-Row Functions.
Comparing Numeric Values If Val(Text1.Text) = MaxPrice Then (Is the current numeric value stored in the Text property of Text1 equal to the value stored.
Selection Structures ; String Functions. If…Then…Else If condition Then [instructions when the condition is true] [Else [instructions when the condition.
JavaScript, Third Edition
Chapter 9 Creating Formulas that Manipulate Text Microsoft Office Excel 2003.
Topics Built-in functions commonly used in business applications String functions and formatting Dates and Times Formatting Data for output Error handling.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Manipulation Masterclass By the VB Gods. In this masterclass, we will learn how to use some of the string manipulation function such as Len, Right, Left,
CSCI 3327 Visual Basic Chapter 12: Strings, Characters and Regular Expressions UTPA – Fall 2011.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL.
Chapter 5 Selected Single-Row Functions. Chapter Objectives  Use the UPPER, LOWER, and INITCAP functions to change the case of field values and character.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Oracle 11g: SQL Chapter 10 Selected Single-Row Functions.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
Manipulating Strings. What is a string? Data composed of text characters. The data is stored in consecutive bytes of memory. Each byte stores the ASCII.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
Chapter 8: Manipulating Strings
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 24 The String Section.
Chapter 3 Selected Single-Row Functions and Advanced DML & DDL.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Working with Date Variables (Unit 5)
EXPRESSION Transformation. Introduction ►Transformations help to transform the source data according to the requirements of target system and it ensures.
Lecture 8 – SQL Joins – assemble new views from existing tables INNER JOIN’s The Cartesian Product Theta Joins and Equi-joins Self Joins Natural Join.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
More Strings CS303E: Elements of Computers and Programming.
1 Microsoft® Visual Basic®.NET Language # 1. 2 Variable Declaring Variable Dim {VariableName} As {Type} Dim {VariableName} As {Type} = {value} Example:
H2K Infosys is business based in Atlanta, Georgia – United States Providing Online IT training services world wide. USA - +1-(770) ,
CECS 5020 Computers in Education Visual Basic Variables and Constants.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Characters and Strings
1 Inside Module 8 Extracting Data Page n Using the Extract command2 n Coercion3 n $-functions4 n Extract from a table7.
[ ] Square brackets enclose syntax options { } Braces enclose items of which only one is required | A vertical bar denotes options … Three dots indicate.
Strings PART I STRINGS, DATES, AND TIMES. FUNDAMENTALS OF CHARATERS AND STRINGS VB represents characters using American National Standards Institute(ANSI)
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions.
Gollis University Faculty of Computer Engineering Chapter Five: Retrieval, Functions Instructor: Mukhtar M Ali “Hakaale” BCS.
1 VB-06-String Manipulation Mar 03, 2002 String Function VISUAL BASIC.
CSC 162 Visual Basic I Programming. String Functions LTrim( string ) –Removes leading spaces from the left side of string RTrim( string ) –Removes trailing.
13/06/ Strings Left, Right and Trim. 213/06/2016 Learning Objectives Explain what the Left, Right and Trim functions do.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
String and Lists Dr. José M. Reyes Álamo.
Chapter 10 Selected Single-Row Functions Oracle 10g: SQL
MARK CARPENTER, Ph.D. Introduction to SAS Programming and Applications
2. Understanding VB Variables
SQL Text Manipulation Farrokh Alemi, Ph.D.
Chapter 6 Variables What is VBScript?
CIS16 Application Development and Programming using Visual Basic.net
String and Lists Dr. José M. Reyes Álamo.
String Manipulation and More Controls
Sub Procedures and Functions
VBScript Session 10.
Topics Basic String Operations String Slicing
Introduction to Computer Science
MapInfo SQL String Functions
Lecture 5 SQL FUNCTIONS.
Topics Basic String Operations String Slicing
Topics Basic String Operations String Slicing
Presentation transcript:

BACS 287 Visual Basic String Manipulation

BACS 287 Visual Basic Strings In Visual Basic, a “string” is a series of text, numbers, and special characters. Strings are stored in string variables. String values are always enclosed in quotation marks. There is only 1 built-in operator that works on strings (the ‘&’ operator).

BACS 287 String Characteristics  All strings in Visual Basic are variable-length strings.  A variable-length string can contain up to approximately 2 billion characters.  Generally speaking, string variables can contain any character represented on the keyboard.

BACS 287 String Examples The following are examples of defining and manipulating strings. Dim strInput as string = “ABC 123” Dim strOutput as string = “def,/ 56r” Dim strX as string strX = strInput & strOutput Results in strX value of “ABC 123def,/ 56r”

BACS 287 Operations on Strings There are 2 basic ways to manipulate strings in Visual Basic – Using built-in string functions – Using string methods Methods associated with string variables are the more modern way to manipulate strings You can use either methods or functions; both achieve the same results; however, methods are the preferred way.

BACS 287 String Methods There are several methods automatically associated with all string variables. Determine current string state: string.Length – returns the number of characters in a string string.Compare – determine if two strings are the same string.IsNullOrEmpty – determine if a string is null or empty string.StartsWIth – determine if a group of characters is at start of a string string.EndsWith – determine if a group of characters is at end of a string string.Contains – determine if a string contains a group of characters (Boolean) string.IndexOf – find the starting location of a substring within the string

BACS 287 String Methods Modify edges of string: string.TrimStart – removes leading characters from a string string.TrimEnd – removes trailing characters from a string string.Trim – removes leading and trailing characters from a string string.PadLeft – adds characters to the beginning of a string string.PadRight – adds characters to the end of a string Convert string to different format: string.ToUpper – convert all characters to upper case string.ToLower – convert all characters to lower case

BACS 287 String Methods Add / Remove characters in existing string: string.Remove – removes characters from anywhere in a string string.Substring – extract a substring from the string string.Replace – replaces characters with other characters in a string string.Insert – adds characters within a string Manipulate string into different string / array: string.CopyTo – copies a specified number of characters to an array string.Split – returns a string array of delimited elements from the original string

BACS 287 String Functions Most of the same string manipulations can be performed with system defined functions. INSTR - returns the position of the first occurrence of one string within another MID - Returns the specified number of characters from a string LEN - Returns the number of characters in a string LEFT / RIGHT - Returns the specified number of characters from the left/right side of a string. UCASE / LCASE - Returns a string that has been converted to uppercase or lowercase

BACS 287 String Functions TRIM / RTRIM / LTRIM - Returns a copy of a string without leading spaces (Ltrim), trailing spaces (Rtrim), or both leading and trailing spaces (Trim). FORMAT - Formats an expression according to instructions contained in a format expression.

BACS 287 InStr Function The InStr function is used when you want to find the first occurrence of one string within another. It returns the position of where the match is found, 0 if it is not found. Syntax: InStr (start, string1,string2,[compare]) NOTE: You start counting at 1 with this command

BACS 287 InStr Function Example SearchString ="XXpXXpXXPX XP“ ' String to search in (note that position 11 is a space). SearchChar = "P"' Search for "P". IntStartPos = 7‘ Start in position 7 ' A textual comparison starting at position 4. Returns 6 MyPos = InStr(4, SearchString, SearchChar, 1) ' Comparison is binary by default (last argument is omitted). MyPos = InStr(SearchString, SearchChar)' Returns 9 MyPos = InStr(1, SearchString, " ") ' Returns 11 (started in pos 1) MyPos = InStr(intStartPos, SearchString, "p")' Returns 0 (did not find it)

BACS 287 Mid Function The MID function is used to extract characters from a string. It is commonly used with InStr to pull out the sub-string that was located. Syntax: Mid (string, start, [length])

BACS 287 Mid Function Example MyString = "Mid Function Demo"' Assign text string. FirstWord = Mid(MyString, 1, 3)' Returns "Mid". LastWord = Mid(MyString, 14, 4)' Returns "Demo". MidWords = Mid(MyString, 5)' Returns "Function ' Demo". strOut = Mid(MyString, InStr(1,MyString,”F”), 8) ' Returns “Function” InStr function returns 5

BACS 287 Determine String Length The Length method and the Len function may be used to determine the number of characters in a string. The Len function can also be used to determine the number of bytes to store a variable (somewhat obscure). Syntax: string.length Len (string | variable)

BACS 287 Determine String Length Example Dim MyString as string = “Hello World” Dim MyInt As Integer Dim MyCur As Currency ‘ use method MyLen = MyString.Length‘ returns 11 ‘ use function MyLen = Len(MyString)' Returns 11. MyLen = Len(MyInt)' Returns 2. MyLen = Len(MyCur)' Returns 8.

BACS 287 Modify String Edges Operations The Left and Right functions are used to extract characters from the left and right side of a string. The original string is not modified Syntax: Left (string,length) Right (string,length)

BACS 287 Left / Right Example AnyString = "Hello World"' Define string. MyStr = Left(AnyString, 1)' Returns "H". MyStr = Left(AnyString, 7)' Returns "Hello W". MyStr = Left(AnyString, 20)' Returns "Hello World". MyStr = Right(AnyString, 1)' Returns "d". MyStr = Right(AnyString, 6)' Returns " World". MyStr = Right(AnyString, 20) ' Returns "Hello World".

BACS 287 Ucase / Lcase Function The Ucase and Lcase functions return a string with the case changed to upper or lower. Syntax: Ucase (string) Lcase (string)

BACS 287 Ucase / Lcase Example strInput = "Hello World 1234" ' String to convert. UpperCase = UCase(strInput) ' Returns "HELLO WORLD 1234". LowerCase = LCase(strInput) ' Returns ”hello world 1234".

BACS 287 Ltrim / Rtrim / Trim Function The Ltrim, Rtrim, and Trim functions remove leading spaces, trailing spaces, and both leading and trailing spaces. Syntax: LTrim (string) RTrim (string) Trim (string)

BACS 287 LTrim / Rtrim / Trim Function MyString = " "' Initialize string TrimString = LTrim(MyString)' TrimString = " ". TrimString = RTrim(MyString)' TrimString = " ". TrimString = LTrim(RTrim(MyString)) ' TrimString = " ". ' Using the Trim function alone achieves the same result as the previous example. TrimString = Trim(MyString)' TrimString = " ".

BACS 287 Format Function The Format function is used format a string expression based upon instructions given in a “format expression”. The rules for this function are very complex and are best learned via the VB help screens. Syntax: Format(expression,[format,[firstdayof week,[firstweekofyear]]])

BACS 287 Format Function Examples MyTime = #17:04:23# MyDate = #January 27, 1999# ' Returns current system time in the system-defined long time format. MyStr = Format(Time, "Long Time") ' Returns current system date in the system-defined long date format. MyStr = Format(Date, "Long Date") MyStr = Format(MyTime, "h:m:s")' Returns "17:4:23". MyStr = Format(MyTime, "hh:mm:ss AMPM")' Returns "05:04:23 PM". MyStr = Format(MyDate, "dddd, mmm d yyyy")' Returns "Wednesday, ' Jan ".

BACS 287 Format Function Examples ' If format is not supplied, a string is returned. MyStr = Format(23)' Returns "23". ' User-defined formats. MyStr = Format(5459.4, "##,##0.00")' Returns "5,459.40". MyStr = Format(334.9, "###0.00")' Returns "334.90". MyStr = Format(5, "0.00%")' Returns "500.00%". MyStr = Format("HELLO", "<")' Returns "hello". MyStr = Format("This is it", ">")' Returns "THIS IS IT". See Visual Basic help screens for more format expression details.

BACS 287 Nesting String Functions You can nest string functions inside each other. The innermost function is performed first and the outermost is performed last. Example: Dim MyString as string = ” nested function demo “ strX = Mid(Ucase(Trim(MyString)), InStr(1,Ucase(Trim(MyString)),”UNC”), 8) ' Returns “UNCTION ”

BACS 287 Nesting String Functions This also works with string methods. Example: Dim MyString as string =” nested function demo “ strX = MyString.Substring((MyString.Trim.ToUpper.IndexOf("UNC", 0) + 2), 8).ToUpper ' Returns “UNCTION ”