CodeGym /Courses /Java Syntax Zero /Examples of working with strings

Examples of working with strings

Java Syntax Zero
Level 2 , Lesson 6
Available

1. Concatenation (merging strings)

There's this slick and simple thing that you can do with strings in Java: you can glue them together. This operation is called concatenation. Here's how we remember it: Con-Cat-en-Nation. It is often called "joining strings" or "combining strings".

To concatenate two lines, you use the + sign. It's very easy:

"value1" + "value2"
Concatenating two strings

Examples:

Statement Note
String name = "Steve" + "Steve";
name contains the string SteveSteve
String city = "New York" + "Steve";
city contains the string New YorkSteve
String message = "Hello! " + "Steve";
message contains the string Hello! Steve

And, of course, you can join lots of strings at the same time, and you can also join strings and variables.

Examples:

Statement Note
String name = "Steve";
String city = "New York";
String message = "Hello!" + city + name + city;
name contains the string Steve
city contains the string New York
message contains the string
Hello!New YorkSteveNew York

In the last example, you can see that the text in the message is difficult to read, because it is missing spaces. To indicate one or more spaces, you just need to write them in code and then wrap them in double quotes. It's easier than it sounds:

" "
A string containing one space

By the way, if you don't put any spaces between the quotes (i.e. you write two double quotes in a row), you get the so-called "empty string":

""
Empty string

On the one hand, it seems we have a string. But on the other hand, when we display this string, nothing is displayed. And when we join it with other strings, nothing happens. It's kind of like a zero in addition, only for strings.


2
Task
New Java Syntax, level 2, lesson 6
Locked
Contract
The rule "Always read the terms of the contract!" seems simple enough, but so many people get burned because they don't follow it! But programmers are not like that. They always carefully study project conditions/specifications and only then do they draw conclusions, make plans, and start working. Let's practice a useful skill: we'll change the terms of the contract to be more favorable.

2. Converting to a string

As mentioned above, Java developers have made sure that absolutely every variable, object, and expression in Java can be converted to the String type.

What's more, this happens automatically when we concatenate a String with some other type. Examples:

Statement Note
int a = 5;
String name = "Steve" + a;
name contains the string Steve5
int a = 5;
String city = a + "New York" + a;
city contains the string 5New York5
int number = 10;
String code = "Yo";
String message = "Hello! " + number + code;
message contains the string Hello! 10Yo

In all three instances, we calmly combined int and String variables, and the result is always a String.

You can't perform arithmetic operations with the String type. Even if the entire string consists of digits.

Examples:

Statement Note
int a = 5;
String name = "1" + a;
name contains the string 15
int a = 5;
String city = a + "9" + a;
city contains the string 595
int number = 10;
String code = "10";
String message = "" + number + code;
message contains the string 1010

The plus operations are executed from left to right, so the result may be somewhat unexpected. Example:

Statement Note
int a = 5;
String name = a + a + "1" + a;
name contains the string 1015
Order of operations: ((a + a) + "1") + a

3. Converting a string to a number

Converting a number to a string in Java is as easy as concatenating it to an empty string:

String str"" + number;
Converting a number to a string

But what if you need to convert a string to a number? Well, not every string can be converted to a number. But if the string consists only of numbers, then you can. There is a special method for this in the Integer class.

The corresponding statement looks like this:

int x = Integer.parseInt(string);

Where int x is the declaration of an x integer variable, and string is a string that represents a number (i.e. a string consisting of digits).

Examples:

Statement Note
String str = "123";
int number = Integer.parseInt(str);
number contains the number 123;
int number = Integer.parseInt("321");
number contains the number 321
int number = Integer.parseInt("321" + 0);
number contains the number 3210
int number = "321";
This won't compile: the variable is an int, but the value is a String


4. Some methods for working with strings

And finally, I would like to talk about several methods of the String class.

length() method

The length() method lets you get the length of a string, i.e. how many characters it contains.

Examples:

Statement Note
String name = "Rome";
int count = name.length();
count contains the value 4
int count = "".length();
count contains the value 0
String name = "Rom";
int count = (name + 12).length();
count contains the value 5

You can call these methods on anything whose type is String, even an expression:

(name + 12).length()
Calling the length() method on an expression whose type is String

toLowerCase() method

The toLowerCase() method lets you convert all characters in a string to lowercase:

Examples:

Statement Note
String name = "Rom";
String name2 = name.toLowerCase();
name2 contains the string rom
String name = "".toLowerCase();
name contains an empty string
String name = "ROM123";
String name2 = name.toLowerCase();
name2 contains the string rom123

toUpperCase() method

The toUpperCase() method lets you convert all characters in a string to uppercase:

Examples:

Statement Note
String name = "Rom";
String name2 = name.toUpperCase();
name2 contains the string ROM
String name = "rom123";
String name2 = name.toUpperCase();
name2 contains the string ROM123

Comments (24)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
TinCanSailor Level 3, Albuquerque, United States
10 April 2024
System.out.println("RESUME".toLowerCase()); System.out.println(("TITLE: " + title).toLowerCase()); System.out.println(("DEGREE: " + degree).toLowerCase()); System.out.println(("CAREER: " + career).toLowerCase());
Anonymous #11320702 Level 2, Spain
31 March 2023
Me too. I can't continue because of the premiums. This would have been great.
ilia lartsuliani Level 2, Georgia
27 January 2023
why is there so much premium tasks, i'm wasting my levels
Leho Level 3, Philippines
25 December 2022
hope someone gift some 1 month sub :)
Seras88 Level 4, Atlanta, United States
13 December 2022
Can someone explain to me how count is 5. I am a bit sick and I'm out of it. thanks

String name = "Rom";
int count = (name + 12).length();
Anonymous #11225109 Level 5, United States of America, United States
23 December 2022
It looks like 12 has two characters 1and 2, added to ROM, you get 5 characters. There's no difference between numbers and letters when it comes to characters counting.
Seras88 Level 4, Atlanta, United States
23 December 2022
OMG, it's so obvious now. Thank you!
juro kasumi Level 2, United States of America, United States
31 December 2022
Maybe it's 15 not 5. Idk
Nishith Level 6, Vadodara, India
14 June 2023
here variable name hase length of 3 ,then in second variable assume that we have "length of first variable which is 3" plus "length of 12 which will be 2" so 3 + 2 is equals to 5.
T Scott Level 2, United States
12 January 2024
i think it goes but the number of characters and not the actual number so Rom = 3 & 12 = 2. ex : 1000 = 4
Elvis Level 6, Port Harcourt , Nigeria
3 April 2024
this a bit late cause its been more than a year already but the stirng name is rom, which has 3 characters, and 12 has 2 characters therefore there are 5 characters
bram oers Level 9, Vilvoorde, Belgium
17 November 2022
So, for parseint() we put stuff between te brackets. but for length() we put the stuff before the method. Probably it will be covered later, but... can someone explain me why now already? :-)
Data Level 4, Winterberg, Germany
8 November 2022
I am sad about .. i have to do System.out.println((caps + "they know I mean business").toUpperCase()); but System.out.println(caps.toUpperCase() + "they know I mean business".toUpperCase()); worked as well, same output but attempt to be wront :( same with lowerCase method. I know, its twice written, and the correct one is more slim, but the other would give the correct answer as well ....
SethuRama Subramanian Level 4, India
3 November 2022
GOMI GOMU NO BAZOOKA! loved, having seen a Onepiece reference!!
Anonymous#183 Level 3, Canada
22 November 2022
bro likes anime 💀
Anonymous #11313983 Level 3, United States of America, United States
16 March 2023
I did not know where that was from....thank you for clarifying.
tabitha Level 3, Philippines
1 November 2022
ngl this one is the hardest for me yet
Anonymous #11313983 Level 3, United States of America, United States
16 March 2023
I thought so too initially but follow the examples and use a few () as needed...
Anonymous #11149234 Level 6, United States of America, United States
21 October 2022
I did 4 attempts to make it work for the uppercase lesson. I wasnt following the instructions :(