Java Programming 1: Strings and Characters
Java Programming 1: Strings and Characters
Lecture 6
Strings and Characters
Fundamentals of Characters and Strings
• A character:
• Single lower case letter : a – z
• Single upper case letter : A – Z
• Digits : 0 - 9
• Special characters : +, -, *, /, $, ? and so on
• To declare a character:
char a = ‘x’;
• To declare a string:
String b = “This is a string”;
Declaring character and some character class function
public class string1
{
public static void main (String[] args)
{
char a = 'f';
char b = '3';
int c = 3;
char d = ' ';
char e = 'G';
b = "The length of \"" + b + "\" is " + b.length(); //string.length() is the total length of the string
System.out.println(b);
a.getChars(6,11,charArray,0); //string.getChars() collects a set of characters from string and insert into a char array.
for (int x=0;x<charArray.length;x++)
{
System.out.print(charArray[x]);
}
}
Functions from previous example
• “Hello World”
stringname.length()
Size of the array
which is 11
(0) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
H e l l o W o r l d
stringname.getChar(6,11,charArray,0)
stringname.charAt(1) This function will get characters from
Character at index 1 6th index to (11 minus 1) index.
of string = ‘e’ It will then store it in a character array
called charArray starting from index 0
Comparing strings
Refer to comparingstring.java
• There is a difference when you create:
1. String a = new string (“hello”);
2. String a = “hello”;
• stringname.equals (“text”);
• This function compares if the “text” is equal to the data in stringname.
• Can be used to copy “text” in an object or variable.
• stringname == “text”
• This is used to check if stringname IS “text”
• Will return false if stringname is an object (because the object is just a copy not actually “text”)
Comparing strings
• stringname.equalsIgnoreCase (“text”);
• Compares the stringname with “text” ignoring case
Refer to checkstring.java
• stringname.startsWith (“text”)
• This function compares if the data in stringname has starting characters that
matches “text”
• stringname.startsWith (“text”,indexnum)
• This function compares if the data in stringname has starting characters that
matches “text” beginning from indexnum.
• stringname.endsWith (“text”)
• This function compares if the data in stringname has ending characters that
matches “text”
String searching functions
Refer to searchstring.java
• stringname.indexOf (“text”, startingindex)
• This function returns the first index of “text” and the search starts from
startingindex.
• Startingindex parameter is optional
Refer to extractstring.java
• stringname.substring (startingindex, endingindex)
• This function will return a substring starting from the startingindex up to the
value before the endingindex
String concatenation functions
System.out.println (a.concat(b));
System.out.println (a);
}
}
stringname.concat(stringnameb)
• This function will concatenate stringname to stringnameb
• Note that stringname or stringnameb does not change value at all.
String trim function
System.out.println (a.trim());
}
}
a = a.replaceAll(“\\s”, “”);
System.out.print (a);
}
}
stringname.replaceAll(“regex”, “newchar”)
• This function will replace all regex values found in stringname with “newchar”
• Important regex:
i. . all characters
ii. \d all digits ; \D all non digits
iii. \s all whitespaces ; \S all non whitespaces
iv. \w all word characters; \W all non word characters
To use scanner method to input string
1. Write a program for the user to input a string. Check if the string is
palindrome.
• Palindrome example: reviver
2. Write a program for the user to input a string. Check if the string is
an anagram.
• Anagram example: roots is an anagram of torso.
3. Write a program for the user to input a string. Use bubble sort,
selection sort and insertion sort to sort out the characters in the
string.
Java string practice 2