SlideShare uma empresa Scribd logo
1 de 13
STRINGS
String class
The String class represents character strings. All string literals in Java
 programs, such as "abc", are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are
 created.
For example:
  String str = "abc"; is equivalent to:
  char data[] = {'a', 'b', 'c'};
  String str = new String(data);
Methods
The class String includes methods for
  examining individual characters of the sequence,
  for comparing strings,
  for searching strings,
  for extracting substrings, and
  for creating a copy of a string
  with all characters translated to uppercase or to lowercase
Concatenation
String name = “Saira";
String introduction = "Hello there, my name is " + name;
concat(String str)
Concatenates the specified string to the end of this string.
public class test
{
  public static void main(String [] args)
  {
           String str = "hello";
           String str1 = "World";
           String str2=str.concat(str1);
           System.out.println(str2);
  }
}
Character at given position
getting the character at a given position within the string --
  charAt()
   public class test
   {
         public static void main(String [] args)
         {
               String str = "hello";
               char c= str.charAt(1);
               System.out.print(c);
         }
   }
getting the number of characters in a
string -- length()
public class test
{
      public static void main(String [] args)
      {
            String str = "hello";
            int x= str.length();
            System.out.print(x);
      }
}
extracting a substring from a string --
substring()
public class test
{
      public static void main(String [] args)
      {
            String str = "hello World";
            String str1= str.substring(2,8);
            System.out.print(str1);
      }
}
Compare two strings for equality – equals()
public class test
{
     public static void main(String [] args)
     {
           String str = "hello";
           String str1 ="hello";
           System.out.println(str.equals(str1));
     }
}
Compare two strings – compareTo()
=0 indicate equality
>0 indicate calling string follows argument string
<0 indicate calling string precedes argument string

 public class test
 {
        public static void main(String [] args)
        {
                String str = "hello";
                String str1 ="abcde";
                System.out.println(str.compareTo(str1));
        }
 }
Tests if this string ends with the specified suffix.
public class test
{
      public static void main(String [] args)
      {
            String str = "hello world";
            String str1 ="ld";
            System.out.println(str.endsWith(str1));
      }
}
Replacing all occurrences of oldChar in string with
newChar
public class test
{
      public static void main(String [] args)
      {
            String str = "hello world";
            String str1 = str.replace('l','x');
            System.out.println(str1);
      }
}
Converts String to character array
public class test
{
      public static void main(String [] args)
      {
            String str = "hello world";
            char []array =str.toCharArray();
            System.out.println(array);
            System.out.println(array[0]);
      }
}
Converts all characters to lowercase or uppercase

public class test
{
      public static void main(String [] args)
      {
            String str = "hello world";
            String str1 = str.toUpperCase();
            String str2= "HHHElloooo";
            String str3 = str2.toLowerCase();
            System.out.println(str1);
            System.out.println(str3);
      }
}

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
 
P3 2017 python_regexes
P3 2017 python_regexesP3 2017 python_regexes
P3 2017 python_regexes
 
Language R
Language RLanguage R
Language R
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
 
scala-101
scala-101scala-101
scala-101
 
11 15 (doubly linked list)
11 15 (doubly linked list)11 15 (doubly linked list)
11 15 (doubly linked list)
 
Sequence Types in Python Programming
Sequence Types in Python ProgrammingSequence Types in Python Programming
Sequence Types in Python Programming
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data science
 
Shell sort
Shell sortShell sort
Shell sort
 
Python list
Python listPython list
Python list
 
Python data handling notes
Python data handling notesPython data handling notes
Python data handling notes
 
Basic and logical implementation of r language
Basic and logical implementation of r language Basic and logical implementation of r language
Basic and logical implementation of r language
 
1. python
1. python1. python
1. python
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scala
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Using Java Streams
Using Java StreamsUsing Java Streams
Using Java Streams
 
Austen x talk
Austen x talkAusten x talk
Austen x talk
 

Destaque

Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Chloe Main
 

Destaque (8)

Comp102 lec 8
Comp102   lec 8Comp102   lec 8
Comp102 lec 8
 
Comp102 lec 4
Comp102   lec 4Comp102   lec 4
Comp102 lec 4
 
Comp102 lec 5.1
Comp102   lec 5.1Comp102   lec 5.1
Comp102 lec 5.1
 
Comp102 lec 3
Comp102   lec 3Comp102   lec 3
Comp102 lec 3
 
Comp102 lec 5.0
Comp102   lec 5.0Comp102   lec 5.0
Comp102 lec 5.0
 
Comp102 lec 7
Comp102   lec 7Comp102   lec 7
Comp102 lec 7
 
Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!
 
Comp102 lec 10
Comp102   lec 10Comp102   lec 10
Comp102 lec 10
 

Semelhante a Comp102 lec 9

Semelhante a Comp102 lec 9 (20)

Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
 
Strings part2
Strings part2Strings part2
Strings part2
 
Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptx
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
Strings
StringsStrings
Strings
 
JDK 8
JDK 8JDK 8
JDK 8
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Strings.ppt
Strings.pptStrings.ppt
Strings.ppt
 
String to numeric conversions-cvmanik
String to numeric conversions-cvmanikString to numeric conversions-cvmanik
String to numeric conversions-cvmanik
 
The Ring programming language version 1.5.3 book - Part 35 of 184
The Ring programming language version 1.5.3 book - Part 35 of 184The Ring programming language version 1.5.3 book - Part 35 of 184
The Ring programming language version 1.5.3 book - Part 35 of 184
 
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper class
 
The Ring programming language version 1.3 book - Part 26 of 88
The Ring programming language version 1.3 book - Part 26 of 88The Ring programming language version 1.3 book - Part 26 of 88
The Ring programming language version 1.3 book - Part 26 of 88
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
Strings
StringsStrings
Strings
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Strings
StringsStrings
Strings
 
Strings
StringsStrings
Strings
 
Strings
StringsStrings
Strings
 
Strings
StringsStrings
Strings
 
C q 3
C q 3C q 3
C q 3
 

Comp102 lec 9

  • 2. String class The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. For example: String str = "abc"; is equivalent to: char data[] = {'a', 'b', 'c'}; String str = new String(data);
  • 3. Methods The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase
  • 4. Concatenation String name = “Saira"; String introduction = "Hello there, my name is " + name; concat(String str) Concatenates the specified string to the end of this string. public class test { public static void main(String [] args) { String str = "hello"; String str1 = "World"; String str2=str.concat(str1); System.out.println(str2); } }
  • 5. Character at given position getting the character at a given position within the string -- charAt() public class test { public static void main(String [] args) { String str = "hello"; char c= str.charAt(1); System.out.print(c); } }
  • 6. getting the number of characters in a string -- length() public class test { public static void main(String [] args) { String str = "hello"; int x= str.length(); System.out.print(x); } }
  • 7. extracting a substring from a string -- substring() public class test { public static void main(String [] args) { String str = "hello World"; String str1= str.substring(2,8); System.out.print(str1); } }
  • 8. Compare two strings for equality – equals() public class test { public static void main(String [] args) { String str = "hello"; String str1 ="hello"; System.out.println(str.equals(str1)); } }
  • 9. Compare two strings – compareTo() =0 indicate equality >0 indicate calling string follows argument string <0 indicate calling string precedes argument string public class test { public static void main(String [] args) { String str = "hello"; String str1 ="abcde"; System.out.println(str.compareTo(str1)); } }
  • 10. Tests if this string ends with the specified suffix. public class test { public static void main(String [] args) { String str = "hello world"; String str1 ="ld"; System.out.println(str.endsWith(str1)); } }
  • 11. Replacing all occurrences of oldChar in string with newChar public class test { public static void main(String [] args) { String str = "hello world"; String str1 = str.replace('l','x'); System.out.println(str1); } }
  • 12. Converts String to character array public class test { public static void main(String [] args) { String str = "hello world"; char []array =str.toCharArray(); System.out.println(array); System.out.println(array[0]); } }
  • 13. Converts all characters to lowercase or uppercase public class test { public static void main(String [] args) { String str = "hello world"; String str1 = str.toUpperCase(); String str2= "HHHElloooo"; String str3 = str2.toLowerCase(); System.out.println(str1); System.out.println(str3); } }