3.1.1.7 String-handling operations in a programming language. Flashcards
Define the purpose and syntax of the Length method.
Purpose: To count the characters in a String.
Syntax:
String name = “Hello”;
int j = name.length( );
Define the purpose and syntax of position.
Purpose: It is possible to determine which character features at a position within a String.
Syntax: String myStr = “Hello purple chicken”;
System.out.println(myStr.indexOf(“purple”);
Define the purpose and syntax of substring.
Purpose: The substring method extracts the characters from a String, between two specified indices, and returns the new sub string.
Syntax:
string.substring(start, end);
Define the purpose and syntax of Concatenation.
Purpose: Used to join two or more Strings. Does not change the existing Strings, but returns a new String containing the text of the joined Strings.
Syntax:
var str1 = "Hello"; var str2 = "World!"; var res = str1.concat(str2);
Define the purpose and syntax of Character → Character code.
Purpose: Get a specific character from a String
Syntax:
String s = "hello"; char c = s.charAt(0); //returns h
Define the purpose and syntax of character code → character.
Purpose: Convert a char to String.
Syntax:
character.toString( );
String to Integer.`
String.toInt( );
String to float.
String s=Float.toString(f);
Define the syntax for converting Date/Time to String.
Date date = Calendar.getInstance().getTime();
DateFormat dateFormat = new SimpleDateFormat(“yyyy-mm-dd hh:mm:ss”);
String strDate = dateFormat.format(date);
Syntax for String to Date/Time.
public class StringToDateExample1 {
public static void main(String[] args)throws Exception {
String sDate1=”31/12/1998”;
Date date1=new SimpleDateFormat(“dd/MM/yyyy”).parse(sDate1);
System.out.println(sDate1+”\t”+date1);
}