module 1: basic digital skills unit 7: strings · d. string java "tolowercase" & java...

11
This project has been funded with support from the European Commission. This publication reflects the views only of the author, and the Commission cannot be held responsible for any use which may be made of the information contained therein. PROJECT TITLE SILVERCODE PROJECT ACRONYM SILVERCODE DATE OF DELIVERY AUTHORS CATALIN MARTIN EDITORS INTELECTUAL OUTPUT AVAILABILITY OF DELIVERABLE MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS

Upload: others

Post on 08-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS · d. String Java "tolowercase" & Java "touppercase" 7 4. Other useful string methods 8 5. Keep in mind 8 Summary of Key Points (what

This project has been funded with support from the European Commission. This publication reflects the views only of the author, and the Commission cannot be held responsible for any use which may be made of the information contained therein.

PROJECT TITLE SILVERCODE

PROJECT ACRONYM SILVERCODE

DATE OF DELIVERY

AUTHORS CATALIN MARTIN

EDITORS

INTELECTUAL OUTPUT

AVAILABILITY OF DELIVERABLE

MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS

Page 2: MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS · d. String Java "tolowercase" & Java "touppercase" 7 4. Other useful string methods 8 5. Keep in mind 8 Summary of Key Points (what

Module 1 Unit 7: Basic Digital Skills. Strings

https://www.silvercodeproject.eu/

Table of Contents

Entrance 2

Keywords 2

I. Module/unit overview 2

II. Learning content 2

Introduction (short explanation) 2

Learning Objective 2

1. What is a string 3

2. Constructing a strings 3

3. String methods 4

a. Concatenation is joining of two or more strings. 5

b. String "Length" Method 5

c. String "indexOf" Method 7

d. String Java "tolowercase" & Java "touppercase" 7

4. Other useful string methods 8

5. Keep in mind 8

Summary of Key Points (what did the participant learn) 8

III Conclusion 8

IV Further resources / Additional reading 8

V Self-test questions 9

VI Glossary 9

VII References 9

Page 3: MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS · d. String Java "tolowercase" & Java "touppercase" 7 4. Other useful string methods 8 5. Keep in mind 8 Summary of Key Points (what

Module 1 Unit 7: Basic Digital Skills. Strings

https://www.silvercodeproject.eu/

Module 1 /unit 7: Basic Digital Skills. Strings _____________________________________________________________________

Entrance Keywords

(list of relative Keywords – up to six) – do not appear in the table of contents but simply immediately after the heading of the component)

I. Module/unit overview

Learning outcomes1 As a result of engaging with the materials in this module, learners are intended to

achieve the following learning outcomes:

Knowledge:

Competences:

Time schedule

Structure

II. Learning content Strings are used for memorizing and processing pieces of small and large texts. Strings are arrays of characters or vector of characters that are often used in programming in order to display messages or to compose a reply to a complex computation task.

Introduction (short explanation)

Learning Objective: What you will learn in this module/unit?

The student will learn the structure of a string and basic operations with strings that are already present in most programming languages libraries: JavaScript, Java, C#, C etc. All given examples are for JavaScript.

1 https://europass.cedefop.europa.eu/education-and-training-glossary/l

Page 4: MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS · d. String Java "tolowercase" & Java "touppercase" 7 4. Other useful string methods 8 5. Keep in mind 8 Summary of Key Points (what

Module 1 Unit 7: Basic Digital Skills. Strings

https://www.silvercodeproject.eu/

1. What is a string I mean, give me a guitar, give me a piano, give me a broom and string,

I wouldn't get bored anywhere.

Keith Richards

A JavaScript string is an array of chars gathered together, like the word "Hello", or the phrase "practice makes perfect".

A string is created in the code by writing its chars between double quotes, example "Hello SilverCode Community".

The string first character is located at index 0, the second character at index 1 and so on. For example, the string “Hello, world!” is structured in memory like this:

0 1 2 3 4 5 6 7 8 9 10 11 12

H e l l o , w o R l d !

Usually, a string is delimited internally with a null character, the character having the ASCII code 0, that we do not see explicitly.

2. Constructing a strings A string can be created in a JavaScript program by a simple object declaration:

var greeting=”Hello, world!”;

In this declaration we have the following elements:

“greeting” is the name of the string object, it can be considered as a handle for the string object, we will use it further for several operations;

“Hello, world!” is the value of the string object and may change during the program run.

A string can be created using the String class instantiation:

var greeting=new String(“Hello, world!”);

In this declaration we have the following elements:

“greeting” is the same name of the string object;

“new” is the operator that creates a new object;

“String” is the class that is instantiated resulting the string object;

“Hello, world!” is the argument of the String class constructor.

Using Notepad++ create a file named “example 1 – string creation.html” having the following content:

<script>

var s1="Hello, World!";

console.log(s1);

Page 5: MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS · d. String Java "tolowercase" & Java "touppercase" 7 4. Other useful string methods 8 5. Keep in mind 8 Summary of Key Points (what

Module 1 Unit 7: Basic Digital Skills. Strings

https://www.silvercodeproject.eu/

var s2=new String("Hello, World!");

console.log(s2);

</script>

Then open the file with Google Chrome or Mozilla Firefox. Press F12 to see the console window and then reload the HTML file by pressing F5 and running the small JavaScript program.

The output for those using Google Chrome should be the following:

The first line of the output is the primitive string “Hello, world!”, while the second line is the String object structure, where you can see the position of each character, having the same content “Hello, world!”.

3. String methods

Page 6: MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS · d. String Java "tolowercase" & Java "touppercase" 7 4. Other useful string methods 8 5. Keep in mind 8 Summary of Key Points (what

Module 1 Unit 7: Basic Digital Skills. Strings

https://www.silvercodeproject.eu/

a. Concatenation is joining of two or more strings.

We have two strings var str1 = “SilverCode” and var str2 = “Community”

If we add up these two strings we should be having a result as var str3= “SilverCodeCommunity”.

Check the below code snippet, it explains the two methods to perform string concatenation.

First is using “concat” method of String class and second is using arithmetic “+” operator. Both results in the same output:

var Sample_String = (function () {

function Sample_String() {

}

Sample_String.main = function (args) {

var str1 = "SilverCode";

var str2 = "Community";

var str3 = str1.concat(str2);

console.info(str3);

var str4 = str1 + str2;

console.info(str4);

};

return Sample_String;

}());

Sample_String["__class"] = "Sample_String";

Sample_String.main(null);

b. String "Length" Method

A very important attribute of a string is the length. The length is the number of characters in the string. For example, the “Hello, world!” string length is 13, counting the characters from position 0 to position 12.

Page 7: MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS · d. String Java "tolowercase" & Java "touppercase" 7 4. Other useful string methods 8 5. Keep in mind 8 Summary of Key Points (what

Module 1 Unit 7: Basic Digital Skills. Strings

https://www.silvercodeproject.eu/

In order to access in programing the length of a string we need to call the “.length” property of the string object.

In the next example, create file “example 2 – string length.html” where we will print the length of the different created strings:

<script>

var s1="Hello, world!";

var s2="Silver";

var s3="Silver Code";

console.log(s1);

console.log(s1.length);

console.log(s2);

console.log(s2.length);

console.log(s3);

console.log(s3.length);

</script>

The output should be following:

The length of “Hello, world!” is 13, the length of “Silver” is 6 and the length of “Silver Code” is 11.

Page 8: MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS · d. String Java "tolowercase" & Java "touppercase" 7 4. Other useful string methods 8 5. Keep in mind 8 Summary of Key Points (what

Module 1 Unit 7: Basic Digital Skills. Strings

https://www.silvercodeproject.eu/

c. String "indexOf" Method

If I know the length, how would I find which character is at which position? In short, how will I find the index of a character?

You gave the answer yourself buddy, there is a “indexOf” method that will help you determine the location of a specific character that you specify.

var Sample_String = (function () {

function Sample_String() {

}

Sample_String.main = function (args) {

var str_Sample = "SilverCodeCommunity";

console.info("Character at position 5: " + str_Sample.charAt(5));

console.info("Index of character \'e\': " + str_Sample.indexOf('e'))

;

};

return Sample_String;

}());

Sample_String["__class"] = "Sample_String";

Sample_String.main(null);

d. String Java "to LowerCase" & Java "to UpperCase"

I want my entire String to be shown in lower case or Upper case? Just use the “toLowercase()” or “ToUpperCase()” methods against the Strings that need to be converted.

var Sample_String = (function () {

function Sample_String() {

}

Sample_String.main = function (args) {

var str_Sample = "SilverCodeCommunity";

console.info("Convert to LowerCase: " + str_Sample.toLowerCase());

console.info("Convert to UpperCase: " + str_Sample.toUpperCase());

};

return Sample_String;

}());

Sample_String["__class"] = "Sample_String";

Sample_String.main(null);

Page 9: MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS · d. String Java "tolowercase" & Java "touppercase" 7 4. Other useful string methods 8 5. Keep in mind 8 Summary of Key Points (what

Module 1 Unit 7: Basic Digital Skills. Strings

https://www.silvercodeproject.eu/

4. Other useful string methods replace - Finds substrings and replaces them with another string.

slice -Returns a new string that has part of the original string removed.

trim - Removes whitespace from around the string. Handy when processing user input.

Substring - Returns a portion of a string.

match - Searches for matches in a string using regular expressions.

5. Keep in mind There are two groups of types in JavaScript: primitives and objects. Any value that

isn’t a primitive type is an object.

The primitives are: numbers, strings, booleans, null and undefined. Everything else is an object.

Undefined means that a variable (or property or array item) hasn’t yet been initialized to a value.

null means “no object”.

Test two values for equality using = = or = = =.

If two operands have different types, the equality operator (= =) will try to convert one of the operands into another type before testing for equality.

If two operands have different types, the strict equality operator (= = =) returns false.

You can use = = = if you want to be sure no type conversion happens, however,

Sometimes the type conversion of = = can come in handy.

Type conversion is also used with other operators, like the arithmetic operators and string concatenation.

JavaScript has five falsey values: undefined, null, 0, “” (the empty string) and false. All other values are truthy.

Strings sometimes behave like objects. If you use a property or method on a primitive string, JavaScript will convert the string to an object temporarily, use the property, and then convert it back to a primitive string. This happens behind the scenes so you don’t have to think about it.

The string has many methods that are useful for string manipulation.

Two objects are equal only if the variables containing the object references point to the same object.

Summary of Key Points (what did the participant learn)

III Conclusion

IV Further resources / Additional reading

Page 10: MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS · d. String Java "tolowercase" & Java "touppercase" 7 4. Other useful string methods 8 5. Keep in mind 8 Summary of Key Points (what

Module 1 Unit 7: Basic Digital Skills. Strings

https://www.silvercodeproject.eu/

V Self-test questions 1. A string is a:

a. an array of chars gathered separately; b. an array of chars gathered together; c. calculation method.

2. Which is a string method: a. length b. concatenate c. indexof d. all of the above.

3. Test two values for equality using: a. = b. = = c. = = =

4. We have string str1 = “SilverCode”. Determine at which position is character “C”. 5. Convert in uppercase string str1 = “this is my first experience in coding”. 6. Create three strings the first four letters of the Greek alphabet: “alfa”, “beta”,

“gamma”, and “delta” and print them in the console. 7. Print in the console each Greek letters and each of their length.

VI Glossary

VII References http://www.journaldev.com/1321/java-string-interview-questions-and-answers

https://docs.oracle.com/javase/tutorial/java/data/strings.html

https://www.tutorialspoint.com/java/java_strings.htm

https://www.javatpoint.com/java-string

https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java

https://stackoverflow.com/questions/767372/java-string-equals-versus

http://codingbat.com/doc/java-string-introduction.html

http://www.homeandlearn.co.uk/java/string_variables.html

https://www.guru99.com/java-strings.html

http://www.javapractices.com/topic/TopicAction.do?Id=207

https://dzone.com/articles/java-string-format-examples

https://www.ntu.edu.sg/home/ehchua/programming/java/J3d_String.html

https://www.ntu.edu.sg/home/ehchua/programming/howto/References.html#java

https://www.javascript.com/learn/javascript/strings

Page 11: MODULE 1: BASIC DIGITAL SKILLS UNIT 7: STRINGS · d. String Java "tolowercase" & Java "touppercase" 7 4. Other useful string methods 8 5. Keep in mind 8 Summary of Key Points (what

Module 1 Unit 7: Basic Digital Skills. Strings

https://www.silvercodeproject.eu/

https://www.sitepoint.com/15-javascript-string-functions/

https://www.tutorialspoint.com/javascript/javascript_strings_object.htm

https://www.w3schools.com/js/js_strings.asp

Eric T. Freeman, Elisabeth Robson (2014), Head First JavaScript Programming, Published by O’Reilly Media, Inc, USA.

Learning Objective (at the beginning: What you will learn in this module/unit?)

Summary of key points (at the end: What did you learn?)

Important

Problem/Question (for discussion/reflection)

Background Information

to keep in mind

Advise/Hint/Tip

Definition

Task/To-do

Download