SSJX.CO.UK
Content

Java Strings

Strings

Strings are collections of characters.

String Example

Create a String

String name="Bob";
System.out.println("Hello "+name);

Getting the length of a String

To get the length of a String, you can use the length() method.

String name="Bob";
System.out.println(name.length());

Changing the case of the String

To change your String to upper or lower case, there are the following methods:

String name="Bob";
System.out.println(name.toUpperCase());
System.out.println(name.toLowerCase());

Getting individual characters from a String

To get a particular character from a String, you can use the charAt() method. Like arrays, Strings start from zero:

String txt="shield";
for(var i=0;i<txt.length;i++){
	System.out.print(txt.charAt(i)+".");
}

Full Program

With the Java JDK installed, from a command line:

class Agent{
	public static void main(String[] args){	
		String name="Bob";
		String org="shield";
		
		System.out.print(name+", Agent of ");
		
		// Upper case the string...	
		org=org.toUpperCase();
		
		// ...then display letter by letter with a dot after.
		for(int i=0;i<org.length();i++){
			System.out.print(org.charAt(i)+".");
		}
	}
}

Created (04/05/2025)