Difference between revisions of "Java/C2/Strings/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
'''Title of script''': Strings in Java
+
Title of script: Strings in Java
  
'''Author''': TalentSprint
+
Author: TalentSprint
  
 
'''Keywords: '''datatype, char, strings, adding strings, video tutorial
 
'''Keywords: '''datatype, char, strings, adding strings, video tutorial
  
 
+
{| border="1"
 
+
|width="50%"|'''Visual Cue '''
{| style="border-spacing:0;"
+
|width="50%"|Description
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Visual Cue '''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Description'''
+
 
+
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 1
+
|Slide 1
 
+
'''Welcome'''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Strings in Java'''.
+
  
 +
Welcome
 +
|Welcome to the spoken tutorial onStrings in Java.
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 2
+
|Slide 2
  
'''Learning Outcomes'''
+
Learning Outcomes
| style="border:0.035cm solid #000000;padding:0.097cm;"| In this tutorial, you will learn how to  
+
|In this tutorial, you will learn how to
 
+
'''create strings''', '''add strings and perform basic string operations like converting to lower case and upper case.'''
+
  
 +
create strings,add strings and perform basic string operations like converting to lower case and upper case.
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 3
+
|Slide 3
  
'''Tools Used'''
+
Tools Used
| style="border:0.035cm solid #000000;padding:0.097cm;"| For this tutorial we are using  
+
|For this tutorial we are using
  
'''Ubuntu 11.10''', JDK 1.6''' and  
+
Ubuntu 11.10, JDK 1.6''' and
  
 
'''Eclipse 3.7'
 
'''Eclipse 3.7'
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 4
+
|Slide 4
 
+
'''Prerequisites'''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"|  To follow this tutorial you  must have knowledge of '''data types in Java.'''
+
  
 +
Prerequisites
 +
|To follow this tutorial you must have knowledge ofdata types in Java.
  
 
If not, for relevant tutorial please visit our website as shown
 
If not, for relevant tutorial please visit our website as shown
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 5
+
|Slide 5
 
+
'''Strings'''
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''String''' in Java, is a sequence of characters.
+
  
 +
Strings
 +
|String in Java, is a sequence of characters.
  
 
Before starting with '''Strings, '''we will first see the character data type.
 
Before starting with '''Strings, '''we will first see the character data type.
 
  
 
Let us now switch to eclipse
 
Let us now switch to eclipse
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Minimize Slides and open Eclipse
+
|Minimize Slides and open Eclipse
  
 
Eclipse should contain the following code
 
Eclipse should contain the following code
  
'''public class StringDemo{'''
+
public class StringDemo{
  
'''public static void main(String<nowiki>[] </nowiki>args){'''
+
public static void main(String[] args){
  
'''}'''
+
}
  
'''}'''
+
}
| style="border:0.035cm solid #000000;padding:0.097cm;"| we have the eclipse IDE and the skeleton required for the rest of the code.
+
|we have the eclipse IDE and the skeleton required for the rest of the code.
 
+
 
+
We have created a class  '''StringDemo '''and added the main method.
+
  
 +
We have created a class '''StringDemo '''and added the main method.
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Inside the main method, type
+
|Inside the main method, type
  
'''char star = '*';'''
+
char star = '*';
| style="border:0.035cm solid #000000;padding:0.097cm;"| Inside the '''main''' method, type '''''char star '''equal to in single''' '''quotes''' asrteicks star'''''
+
|Inside themain method, typechar star equal to in singlequotes asrteicks star
 
+
 
+
This statement creates a variable with name '''star '''and of the type '''char.'''
+
  
 +
This statement creates a variable with name '''star '''and of the typechar.
  
 
It can store exactly one character.
 
It can store exactly one character.
 
  
 
Let us print a word using a few characters.
 
Let us print a word using a few characters.
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Type after the '''char...'''
+
|Type after thechar...
 
+
 
+
'''char c1 = 'c';'''
+
  
'''char c2 = 'a';'''
+
char c1 = 'c';
  
'''char c3 = 'r';'''
+
char c2 = 'a';
| style="border:0.035cm solid #000000;padding:0.097cm;"| Remove the char line and type  ,
+
  
'''char c1 '''''equal to''''' c'''
+
char c3 = 'r';
 +
|Remove the char line and type ,
  
'''char c2 '''''equal to''''' a'''
+
'char c1''equal to c'''
  
'''char c3 '''''equal to''' ''r'''
+
'char c2''equal to a'''
  
We have created three characters to make the word '''car'''.
+
'''char c3 ''equal tor'
  
 +
We have created three characters to make the wordcar.
  
 
Now let us use them to print the word.
 
Now let us use them to print the word.
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Type after '''char3...'''
+
|Type afterchar3...
  
 +
System.out.print(c1);
  
'''System.out.print(c1);'''
+
System.out.print(c2);
  
'''System.out.print(c2);'''
+
System.out.print(c3);
 
+
'''System.out.print(c3);'''
+
 
+
 
+
Save and Run.
+
  
 +
Save and Run.
  
 
Point to output
 
Point to output
| style="border:0.035cm solid #000000;padding:0.097cm;"|  type,
+
|type,
  
 +
System.out.print(c1);
  
'''System.out.print(c1);'''
+
System.out.print(c2);
  
'''System.out.print(c2);'''
+
System.out.print(c3);
  
'''System.out.print(c3);'''
+
Please note that I’m usingprint instead ofprintln so that all the characters are printed on the same line.
 
+
 
+
Please note that I’m using '''print''' instead of '''println''' so that all the characters are printed on the same line.
+
 
+
 
+
save the file and  run it.
+
  
 +
save the file and run it.
  
 
As we can see, the output is as expected.
 
As we can see, the output is as expected.
 
  
 
But this method only prints the word but does not create one.
 
But this method only prints the word but does not create one.
  
 
+
To create a word, we use theString data type.
To create a word, we use the '''String''' data type.  
+
 
+
  
 
Let us try it out.
 
Let us try it out.
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Remove everything inside the '''main''' function and type
+
|Remove everything inside themain function and type
  
 +
String greet = “Hello Learner”;
 +
|remove everything inside the main method and type
  
'''String greet = “Hello Learner”;'''
+
Type''' String greet'equal to '''Hello Learner''in quotes;'''
| style="border:0.035cm solid #000000;padding:0.097cm;"| remove everything inside the main method and type
+
  
 +
Note that the '''S '''in the data typeString is in uppercase.
  
Type''' String greet '''''equal to '''''Hello Learner '''''in quotes'''''<nowiki>;</nowiki>'''
+
We have use double quotes instead of single quotes as delimiters
 
+
 
+
Note that the '''S '''in the data type '''String''' is in uppercase.
+
 
+
 
+
We have use double quotes instead of single quotes as delimiters  
+
 
+
This statement creates a variable '''greet''' that is of the type '''String'''
+
  
 +
This statement creates a variablegreet that is of the typeString
  
 
Now Let us print the message.
 
Now Let us print the message.
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Type the following
+
|Type the following
  
'''System.out.println(greet);'''
+
System.out.println(greet);
+
Save the file  run it .
+
  
 +
Save the file run it .
  
 
Point to output
 
Point to output
| style="border:0.035cm solid #000000;padding:0.097cm;"| 
+
|System.out.println(greet);
'''System.out.println(greet);'''
+
 
+
Save  the file and run it
+
As we can see, the message has been stored in the variable and it has been printed.
+
  
 +
Save the file and run it As we can see, the message has been stored in the variable and it has been printed.
  
 
Strings can also be added in Java.
 
Strings can also be added in Java.
 
  
 
let us see how to do so.
 
let us see how to do so.
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''“Hello Learner”''' to '''“Hello“'''
+
|Change“Hello Learner” to“Hello“
| style="border:0.035cm solid #000000;padding:0.097cm;"| I'm removing the '''Learner''' from the message.  
+
|I'm removing theLearner from the message.
  
 
We'll store the name in a different variable.
 
We'll store the name in a different variable.
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Type after the '''greet...''' line
+
|Type after thegreet... line
  
'''String name = “Java”;'''
+
String name = “Java”;
  
'''String msg = greet + name<nowiki>;</nowiki>'''
+
String msg = greet + name;
  
 +
changeprintln(greet) toprintln(msg)
  
change '''println(greet)''' to '''println(msg)'''
+
Save and Run.
 
+
 
+
Save and Run.  
+
 
+
  
 
Point to output
 
Point to output
| style="border:0.035cm solid #000000;padding:0.097cm;"| 
+
|'''String name'equal to ''“Java”;
'''String name '''''equal to '''''“Java”;'''
+
 
+
  
 
Now we’ll add the strings to make a message
 
Now we’ll add the strings to make a message
  
 +
'''String msg'equal to'greet'plus ''name;
  
'''String msg '''''equal to '''''greet '''''plus '''''name<nowiki>;</nowiki>'''
+
changeprintln(greet) toprintln(msg) save the file and run it.
 
+
 
+
change '''println(greet)''' to '''println(msg)''' save the file and run it.
+
 
+
  
 
We can see that the output shows the greeting and the name.
 
We can see that the output shows the greeting and the name.
  
 
But there is no space separating them.
 
But there is no space separating them.
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Before '''String greet''' line, type this
+
|BeforeString greet line, type this
  
'''char SPACE = ' ';'''
+
char SPACE = ' ';
  
 +
Changegreet + name togreet + SPACE + name
  
Change '''greet + name''' to '''greet + SPACE + name'''
+
Save and Run.
 
+
 
+
Save and Run.  
+
  
 
Point to output
 
Point to output
| style="border:0.035cm solid #000000;padding:0.097cm;"|so Let us create a space character<nowiki>;</nowiki>
+
|so Let us create a space character;
 
+
 
+
'''char SPACE '''''equal to '''''' ';'''
+
  
 +
'''char SPACE 'equal to '''';
  
 
Note that I have used all uppercase letters in the variable name so that it is clear.
 
Note that I have used all uppercase letters in the variable name so that it is clear.
 
  
 
You can change it as you want.
 
You can change it as you want.
  
 +
Now let us add the space to the message.
  
Now  let us add the space to the message.
+
'''greet'plus'SPACE'plus ''name
  
 +
save the file and run it
  
'''greet '''''plus '''''SPACE '''''plus '''''name'''
+
Now we can see the output is clear and as expected.
 
+
 
+
save the file and run it
+
 
+
Now we can see the output is clear and as expected.
+
 
+
  
 
Let us look at a few basic string operations.
 
Let us look at a few basic string operations.
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Change '''“hello”''' to '''“HeLLo”'''
+
|Change“hello” to“HeLLo”
 
+
Change '''“Java”''' to '''“jAvA”'''
+
  
 +
Change“Java” to“jAvA”
  
 
Save and Run.
 
Save and Run.
 
  
 
Point to output
 
Point to output
 +
|I’m changing a few characters of “Hello” to upper case
  
 
+
and a word “java” to uppercase.
 
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| I’m changing a few characters of “Hello” to upper case
+
 
+
 
+
and a word “java” to uppercase.  
+
 
+
  
 
Often, when users give input, we have values like this, in mixed case.
 
Often, when users give input, we have values like this, in mixed case.
  
 
+
So Let us run the file to see the output.
So Let us run the file to see the output.
+
 
+
  
 
As we can see t he output is not clean.
 
As we can see t he output is not clean.
 
  
 
Let us use the String methods to clean the input.
 
Let us use the String methods to clean the input.
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Add the following code before '''System...'''
+
|Add the following code beforeSystem...
  
 +
greet = greet.toLowerCase();
  
'''greet = greet.toLowerCase();'''
+
name = name.toUpperCase();
 
+
'''name = name.toUpperCase();'''
+
 
+
 
+
Save and Run.
+
  
 +
Save and Run.
  
 
Point to output
 
Point to output
| style="border:0.035cm solid #000000;padding:0.097cm;"|  type,
+
|type,
  
'''greet '''''equal to '''''greet.toLowerCase();'''
+
'''greet'equal to ''greet.toLowerCase();
  
 +
This statement converts each character of the stringgreet to lowercase
  
This statement converts each character of the string '''greet''' to lowercase
+
'''name 'equal to ''name.toUpperCase();
 
+
 
+
'''name '''''equal to '''''name.toUpperCase();'''
+
 
+
  
 
This statement converts each character of the string '''name '''to uppercase.
 
This statement converts each character of the string '''name '''to uppercase.
  
 
+
Save the file and Run  it.
Save the file and Run  it.
+
 
+
  
 
As we can see, the output is now clean after we have used the String methods.
 
As we can see, the output is now clean after we have used the String methods.
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.176cm;"|  
+
|
| style="border:0.035cm solid #000000;padding:0.176cm;"| This is how we create strings and perform string operations.
+
|This is how we create strings and perform string operations.
 
+
  
 
There are many more String methods and
 
There are many more String methods and
  
 
We'll discuss them as we move on to complex topics.
 
We'll discuss them as we move on to complex topics.
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Minimize the '''Eclipse''' window and switch to slides.
+
|Minimize theEclipse window and switch to slides.
 
+
  
 
Slide 6
 
Slide 6
  
'''Summary'''
+
Summary
| style="border:0.035cm solid #000000;padding:0.097cm;"| This brings us to the end of this tutorial.  
+
|This brings us to the end of this tutorial.
  
In this tutorial we have learnt  
+
In this tutorial we have learnt
  
 
'''- how to create strings and addstrings '''
 
'''- how to create strings and addstrings '''
  
'''- few string operations like converting to lower case and upper case'''
+
- few string operations like converting to lower case and upper case
 
+
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 7'''Assignment'''
+
|Slide 7Assignment
 
+
|As a assignment,
 
+
 
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| As a assignment,  
+
 
+
- Read about the '''concat''' method of Strings in Java. - Find out how is it different from adding strings.
+
  
 +
- Read about theconcat method of Strings in Java. - Find out how is it different from adding strings.
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 8'''About the Spoken Tutorial Project'''
+
|Slide 8About the Spoken Tutorial Project
 
+
* Watch the video available at[http://spoken-tutorial.org/What_is_a_Spoken_Tutorial  ][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial http][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial ://][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial spoken][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial -][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial tutorial][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial .][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial org][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial /][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial What][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial _][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial is][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial _][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial a][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial _][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial Spoken][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial _][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial Tutorial]
+
 
+
* It summarises the Spoken Tutorial project
+
 
+
* If you do not have good bandwidth, you can download and watch it
+
 
+
 
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| To know more about the '''Spoken Tutorial''' project, watch the video available at the following link.
+
  
 +
* Watch the video available at[http://spoken-tutorial.org/What_is_a_Spoken_Tutorial ][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial http][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial ://][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial spoken][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial -][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial tutorial][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial .][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial org][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial /][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial What][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial _][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial is][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial _][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial a][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial _][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial Spoken][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial _][http://spoken-tutorial.org/What_is_a_Spoken_Tutorial Tutorial]
 +
** It summarises the Spoken Tutorial project
 +
*** If you do not have good bandwidth, you can download and watch it
 +
|To know more about theSpoken Tutorial project, watch the video available at the following link.
  
 
It summarises the Spoken Tutorial Project.
 
It summarises the Spoken Tutorial Project.
  
 
If you do not have good bandwidth, you can download and watch it
 
If you do not have good bandwidth, you can download and watch it
 
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 9'''Spoken Tutorial Workshops'''The Spoken Tutorial Project Team
+
|Slide 9Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
  
 
* Conducts workshops using spoken tutorials
 
* Conducts workshops using spoken tutorials
 +
** Gives certificates for those who pass an online test
 +
*** org (contact at spoken-tutorial dot org)
 +
|The Spoken Tutorial Project Team.
  
* Gives certificates for those who pass an online test
+
Conducts workshops usingspoken tutorials.
 
+
* For more details, please write to [mailto:contact@spoken-tutorial.org contact][mailto:contact@spoken-tutorial.org @][mailto:contact@spoken-tutorial.org spoken][mailto:contact@spoken-tutorial.org -][mailto:contact@spoken-tutorial.org tutorial][mailto:contact@spoken-tutorial.org .][mailto:contact@spoken-tutorial.org org]
+
 
+
 
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team.
+
 
+
Conducts workshops using '''spoken tutorials'''.
+
 
+
Gives certificates for those who pass an online test. For more details, please write to '''contact AT spoken HYPHEN tutorial DOT org.'''
+
  
 +
Gives certificates for those who pass an online test. For more details, please write tocontact AT spoken HYPHEN tutorial DOT org.
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 10'''Acknowledgement'''
+
|Slide 10Acknowledgement
  
 
* Spoken Tutorial Project is a part of the Talk to a Teacher project
 
* Spoken Tutorial Project is a part of the Talk to a Teacher project
 
+
** It is supported by the National Mission on Education through ICT, MHRD, Government of India
* It is supported by the National Mission on Education through ICT, MHRD, Government of India
+
*** More information on this Mission is available at
 
+
**** [http://spoken-tutorial.org/NMEICT-Intro http][http://spoken-tutorial.org/NMEICT-Intro ://][http://spoken-tutorial.org/NMEICT-Intro spoken][http://spoken-tutorial.org/NMEICT-Intro -][http://spoken-tutorial.org/NMEICT-Intro tutorial][http://spoken-tutorial.org/NMEICT-Intro .][http://spoken-tutorial.org/NMEICT-Intro org][http://spoken-tutorial.org/NMEICT-Intro /][http://spoken-tutorial.org/NMEICT-Intro NMEICT][http://spoken-tutorial.org/NMEICT-Intro -][http://spoken-tutorial.org/NMEICT-Intro Intro]
* More information on this Mission is available at
+
|'''Spoken Tutorial '''Project is a part of theTalk to a Teacher project and is supported by the '''National Mission on Education through ICT, MHRD, Government of India. '''More information on this Mission is available atspoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro
 
+
* [http://spoken-tutorial.org/NMEICT-Intro http][http://spoken-tutorial.org/NMEICT-Intro ://][http://spoken-tutorial.org/NMEICT-Intro spoken][http://spoken-tutorial.org/NMEICT-Intro -][http://spoken-tutorial.org/NMEICT-Intro tutorial][http://spoken-tutorial.org/NMEICT-Intro .][http://spoken-tutorial.org/NMEICT-Intro org][http://spoken-tutorial.org/NMEICT-Intro /][http://spoken-tutorial.org/NMEICT-Intro NMEICT][http://spoken-tutorial.org/NMEICT-Intro -][http://spoken-tutorial.org/NMEICT-Intro Intro]
+
 
+
 
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Spoken Tutorial '''Project is a part of the '''Talk to a Teacher''' project and is supported by the '''National Mission on Education through ICT, MHRD, Government of India. '''More information on this Mission is available at '''spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro'''
+
 
+
 
|-
 
|-
| style="border:0.035cm solid #000000;padding:0.097cm;"| Slide 11'''About the contributor'''
+
|Slide 11About the contributor
 
+
* This tutorial has been contributed by '''TalentSprint'''
+
* '''www.talentsprint.com'''
+
 
+
* Thanks for joining
+
 
+
 
+
| style="border:0.035cm solid #000000;padding:0.097cm;"| This tutorial has been contributed by '''TalentSprint'''. Thanks for joining.
+
 
+
 
+
 
+
  
 +
* This tutorial has been contributed byTalentSprint
 +
* www.talentsprint.com
 +
** Thanks for joining
 +
|This tutorial has been contributed byTalentSprint. Thanks for joining.
 
|}
 
|}

Revision as of 12:34, 25 November 2014

Title of script: Strings in Java

Author: TalentSprint

Keywords: datatype, char, strings, adding strings, video tutorial

Visual Cue Description
Slide 1

Welcome

Welcome to the spoken tutorial onStrings in Java.
Slide 2

Learning Outcomes

In this tutorial, you will learn how to

create strings,add strings and perform basic string operations like converting to lower case and upper case.

Slide 3

Tools Used

For this tutorial we are using

Ubuntu 11.10, JDK 1.6 and

Eclipse 3.7'

Slide 4

Prerequisites

To follow this tutorial you must have knowledge ofdata types in Java.

If not, for relevant tutorial please visit our website as shown

Slide 5

Strings

String in Java, is a sequence of characters.

Before starting with Strings, we will first see the character data type.

Let us now switch to eclipse

Minimize Slides and open Eclipse

Eclipse should contain the following code

public class StringDemo{

public static void main(String[] args){

}

}

we have the eclipse IDE and the skeleton required for the rest of the code.

We have created a class StringDemo and added the main method.

Inside the main method, type

char star = '*';

Inside themain method, typechar star equal to in singlequotes asrteicks star

This statement creates a variable with name star and of the typechar.

It can store exactly one character.

Let us print a word using a few characters.

Type after thechar...

char c1 = 'c';

char c2 = 'a';

char c3 = 'r';

Remove the char line and type ,

'char c1equal to c'

'char c2equal to a'

'char c3 equal tor'

We have created three characters to make the wordcar.

Now let us use them to print the word.

Type afterchar3...

System.out.print(c1);

System.out.print(c2);

System.out.print(c3);

Save and Run.

Point to output

type,

System.out.print(c1);

System.out.print(c2);

System.out.print(c3);

Please note that I’m usingprint instead ofprintln so that all the characters are printed on the same line.

save the file and run it.

As we can see, the output is as expected.

But this method only prints the word but does not create one.

To create a word, we use theString data type.

Let us try it out.

Remove everything inside themain function and type

String greet = “Hello Learner”;

remove everything inside the main method and type

Type' String greet'equal to Hello Learnerin quotes;

Note that the S in the data typeString is in uppercase.

We have use double quotes instead of single quotes as delimiters

This statement creates a variablegreet that is of the typeString

Now Let us print the message.

Type the following

System.out.println(greet);

Save the file run it .

Point to output

System.out.println(greet);

Save the file and run it As we can see, the message has been stored in the variable and it has been printed.

Strings can also be added in Java.

let us see how to do so.

Change“Hello Learner” to“Hello“ I'm removing theLearner from the message.

We'll store the name in a different variable.

Type after thegreet... line

String name = “Java”;

String msg = greet + name;

changeprintln(greet) toprintln(msg)

Save and Run.

Point to output

'String name'equal to “Java”;

Now we’ll add the strings to make a message

'String msg'equal to'greet'plus name;

changeprintln(greet) toprintln(msg) save the file and run it.

We can see that the output shows the greeting and the name.

But there is no space separating them.

BeforeString greet line, type this

char SPACE = ' ';

Changegreet + name togreet + SPACE + name

Save and Run.

Point to output

so Let us create a space character;

char SPACE 'equal to ';

Note that I have used all uppercase letters in the variable name so that it is clear.

You can change it as you want.

Now let us add the space to the message.

'greet'plus'SPACE'plus name

save the file and run it

Now we can see the output is clear and as expected.

Let us look at a few basic string operations.

Change“hello” to“HeLLo”

Change“Java” to“jAvA”

Save and Run.

Point to output

I’m changing a few characters of “Hello” to upper case

and a word “java” to uppercase.

Often, when users give input, we have values like this, in mixed case.

So Let us run the file to see the output.

As we can see t he output is not clean.

Let us use the String methods to clean the input.

Add the following code beforeSystem...

greet = greet.toLowerCase();

name = name.toUpperCase();

Save and Run.

Point to output

type,

'greet'equal to greet.toLowerCase();

This statement converts each character of the stringgreet to lowercase

'name 'equal to name.toUpperCase();

This statement converts each character of the string name to uppercase.

Save the file and Run  it.

As we can see, the output is now clean after we have used the String methods.

This is how we create strings and perform string operations.

There are many more String methods and

We'll discuss them as we move on to complex topics.

Minimize theEclipse window and switch to slides.

Slide 6

Summary

This brings us to the end of this tutorial.

In this tutorial we have learnt

- how to create strings and addstrings

- few string operations like converting to lower case and upper case

Slide 7Assignment As a assignment,

- Read about theconcat method of Strings in Java. - Find out how is it different from adding strings.

Slide 8About the Spoken Tutorial Project To know more about theSpoken Tutorial project, watch the video available at the following link.

It summarises the Spoken Tutorial Project.

If you do not have good bandwidth, you can download and watch it

Slide 9Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
    • Gives certificates for those who pass an online test
      • org (contact at spoken-tutorial dot org)
The Spoken Tutorial Project Team.

Conducts workshops usingspoken tutorials.

Gives certificates for those who pass an online test. For more details, please write tocontact AT spoken HYPHEN tutorial DOT org.

Slide 10Acknowledgement
  • Spoken Tutorial Project is a part of the Talk to a Teacher project
Spoken Tutorial Project is a part of theTalk to a Teacher project and is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available atspoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro
Slide 11About the contributor
  • This tutorial has been contributed byTalentSprint
  • www.talentsprint.com
    • Thanks for joining
This tutorial has been contributed byTalentSprint. Thanks for joining.

Contributors and Content Editors

Ashwini, Chandrika, Sneha