TCL - STRINGS. Boolean value can be represented as 1, yes or true for true and 0, no, or false for false.

Similar documents
LINUX PROGRAMMING AND SCRIPTING

AWK - PRETTY PRINTING

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

Java Basic Datatypees

Variables, Constants, and Data Types

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

Java Notes. 10th ICSE. Saravanan Ganesh

Typescript on LLVM Language Reference Manual

2 nd Week Lecture Notes

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

PYTHON MOCK TEST PYTHON MOCK TEST III

"Hello" " This " + "is String " + "concatenation"

Chapter 8 - Characters and Strings

Chapter 2. Lexical Elements & Operators

Basics of Java Programming

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

Chapter 6 Primitive types

Strings, Strings and characters, String class methods. JAVA Standard Edition

Programming in C++ 4. The lexical basis of C++

PYTHON- AN INNOVATION

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Getting started with Java

Variables and Values

6.096 Introduction to C++ January (IAP) 2009

Princeton University. Computer Science 217: Introduction to Programming Systems. Data Types in C

JME Language Reference Manual

STREAM EDITOR - REGULAR EXPRESSIONS

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

JAVA Programming Fundamentals

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

3 The Building Blocks: Data Types, Literals, and Variables

ARG! Language Reference Manual

MACHINE LEVEL REPRESENTATION OF DATA

RUBY VARIABLES, CONSTANTS AND LITERALS

Data Types and Variables in C language

Number Systems, Scalar Types, and Input and Output

Language Fundamentals Summary

Fundamentals of Programming. Lecture 11: C Characters and Strings

Chapter 2 Working with Data Types and Operators

Visual C# Instructor s Manual Table of Contents

Full file at C How to Program, 6/e Multiple Choice Test Bank

Chapter 3 : Informatics Practices. Class XI ( As per CBSE Board) Python Fundamentals. Visit : python.mykvs.in for regular updates

Topic 3. Big C++ by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

Language Reference Manual

COMS W4115 Programming Languages & Translators GIRAPHE. Language Reference Manual

Lecture 2 Tao Wang 1

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

4 Programming Fundamentals. Introduction to Programming 1 1

Formatted Output Pearson Education, Inc. All rights reserved.

Strings, characters and character literals

Exercises Software Development I. 03 Data Representation. Data types, range of values, internal format, literals. October 22nd, 2014

ML 4 A Lexer for OCaml s Type System

CSC 1107: Structured Programming

Scientific Programming in C V. Strings

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Lecture Set 2: Starting Java

CS 106 Introduction to Computer Science I

Full file at

Basic Elements of C. Staff Incharge: S.Sasirekha

Lecture Set 2: Starting Java

Program Elements -- Introduction

Basic data types. Building blocks of computation

Chapter 2: Using Data

IT 374 C# and Applications/ IT695 C# Data Structures

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Chapter 17. Fundamental Concepts Expressed in JavaScript

ADARSH VIDYA KENDRA NAGERCOIL COMPUTER SCIENCE. Grade: IX C++ PROGRAMMING. Department of Computer Science 1

4. Inputting data or messages to a function is called passing data to the function.

Regular Expressions. Regular expressions are a powerful search-and-replace technique that is widely used in other environments (such as Unix and Perl)

LESSON 4. The DATA TYPE char

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

ITP 342 Mobile App Dev. Strings

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

C: How to Program. Week /May/28

Decaf Language Reference Manual

MATHEMATICAL FUNCTIONS CHARACTERS, AND STRINGS. INTRODUCTION IB DP Computer science Standard Level ICS3U

VLC : Language Reference Manual

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Key Differences Between Python and Java

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

C: How to Program. Week /Mar/05

C++ character set Letters:- A-Z, a-z Digits:- 0 to 9 Special Symbols:- space + - / ( ) [ ] =! = < >, $ # ; :? & White Spaces:- Blank Space, Horizontal

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

ENGR 101 Engineering Design Workshop

Advanced Computer Programming

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Chapter. Let's explore some other fundamental programming concepts

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Chapter 2: Data and Expressions

Operations. Making Things Happen

Chapter 2 Elementary Programming

The Java Language Rules And Tools 3

CSCI 123 Introduction to Programming Concepts in C++

Transcription:

http://www.tutorialspoint.com/tcl-tk/tcl_strings.htm TCL - STRINGS Copyright tutorialspoint.com The primitive data-type of Tcl is string and often we can find quotes on Tcl as string only language. These strings can contain alphanumeric character, just numbers, boolean, or even binary data. Tcl uses 6 bit unicode characters and alphanumeric characters can contain letters including non Latin characters, number or punctuation. Boolean value can be represented as, yes or true for true and 0, no, or false for false. String Representations Unlike other languages, in Tcl, you need not include double quotes when it's only a single word. An example can be, set myvariable hello puts $myvariable When above code is executed, it produces following result. hello When we want to represent multiple strings, we can use either double quotes or curly braces. It is shown below. set myvariable "hello world" puts $myvariable set myvariable {hello world} puts $myvariable When above code is executed, it produces following result. hello world hello world String escape sequence A character literal can be a plain character e. g., x, an escape sequence e. g., \t, or a universal character e. g., \u02c0. There are certain characters in Tcl when they are preceded by a backslash they will have special meaning and they are used to represent like newline \n or tab \t. Here, you have a list of some of such escape sequence codes: Escape sequence Meaning \\ \ character \' ' character \" " character \?? character \a Alert or bell

\b Backspace \f Form feed \n Newline \r Carriage return \t Horizontal tab \v Vertical tab Following is the example to show few escape sequence characters: puts("hello\tworld\n\n"); Hello World String command The list of subcommands for string command are listed in the following table. SN Methods with Description compare string string2 Compares string and string2 lexographically. Returns 0 if equal, - if string comes before string2, else. 2 string string2 Returns the index first occurrence of string in string2. If not found, returns -. 3 index string index Returns the character at index. 4 last string string2 Returns the index last occurrence of string in string2. If not found, returns -. 5 length string Returns the length of string. 6 match pattern string Returns if the string matches the pattern. 7

7 range string index index2 Return the range of characters in string from index to index2. 8 tolower string Returns the lowercase string. 9 toupper string Returns the uppercase string. 0 trim string?trimcharacters? Removes trimcharacters in both ends of string. The default trimcharacters is whitespace. trimleft string?trimcharacters? Removes trimcharacters in left beginning of string. The default trimcharacters is whitespace. 2 trimright string?trimcharacters? Removes trimcharacters in left end of string. The default trimcharacters is whitespace. 3 wordend findstring index Return the index in findstring of the character after the word containing the character at index. 4 wordstart findstring index Return the index in findstring of the first character in the word containing the character at index. Examples of some the commonly used Tcl string sub commands are given below. String comparison set s "Hello" set s2 "World" set s3 "World" puts [string compare s s2] if {[string compare s2 s3] == 0} { puts "String \'s\' and \'s2\' are same."; } if {[string compare s s2] == -} { puts "String \'s\' comes before \'s2\'."; }

if {[string compare s2 s] == } { puts "String \'s2\' comes before \'s\'."; } - String 's' comes before 's2'. String 's2' comes before 's'. Index of string set s "Hello World" set s2 "o" puts "First occurrence of $s2 in s" puts [string first $s2 $s] puts "Character at index 0 in s" puts [string index $s 0] puts "Last occurrence of $s2 in s" puts [string last $s2 $s] puts "Word end index in s" puts [string wordend $s 20] puts "Word start index in s" puts [string wordstart $s 20] First occurrence of o in s 4 Character at index 0 in s H Last occurrence of o in s 7 Word end index in s Word start index in s 6 Length of string set s "Hello World" puts "Length of string s" puts [string length $s] Length of string s Handling cases set s "Hello World" puts "Uppercase string of s" puts [string toupper $s] puts "Lowercase string of s" puts [string tolower $s]

Uppercase string of s HELLO WORLD Lowercase string of s hello world Trimming characters set s "Hello World" set s2 "World" puts "Trim right $s2 in $s" puts [string trimright $s $s2] set s2 "Hello" puts "Trim left $s2 in $s" puts [string trimleft $s $s2] set s " Hello World " set s2 " " puts "Trim characters s on both sides of s2" puts [string trim $s $s2] Trim right World in Hello World Hello Trim left Hello in Hello World World Trim characters s on both sides of s2 Hello World Matching strings set s "test@test.com" set s2 "*@*.com" puts "Matching pattern s2 in s" puts [string match "*@*.com" $s ] puts "Matching pattern tcl in s" puts [string match {tcl} $s] Matching pattern s2 in s Matching pattern tcl in s 0 Append command set s "Hello" append s " World" puts $s Hello World

Format command The following table shows the list of format specifiers available in Tcl. Specifier Use %s String representation %d Integer representation %f Floating point representation %e Floating point representation with mantissa-exponent form %x Hexa decimal representation Some simple examples are given below. puts [format "%f" 43.5] puts [format "%e" 43.5] puts [format "%d %s" 4 tuts] puts [format "%s" "Tcl Language"] puts [format "%x" 40] 43.500000 4.350000e+0 4 tuts Tcl Language 28 Scan command scan command is used for parsing a string based to the format specifier. Some examples are shown below. puts [scan "90" {%[0-9]} m] puts [scan "abc" {%[a-z]} m] puts [scan "abc" {%[A-Z]} m] puts [scan "ABC" {%[A-Z]} m] 0 Loading [MathJax]/jax/output/HTML-CSS/jax.js