Java replacing a character within a string

For the last few hours I’ve been following up on the Introduction to programming course at Udacity and thought that it my be helpful to review what I’ve been learning so far by writing about it. That’s one of the reasons why I started this blog. I think that writing about what I’m currently self-studying can make learning more efficient.

The Java Replace Method

For this exercise I need to:

Image

Hints that were given:

Go to  the replace() method in the javadoc
Verify your answer using BlueJ
Here is an example method call: birthday.getMonth();

To solve the problem I created a new java file with the following code:

String river = “Mississippi”; // Initialize a string variable

System.out.println(river.replace(“i”, “x”); // Replace the letter “i” with the letter “x” by using the replace method

And finally the keyword String before the variable name (in this case river) declares the return type which is string

The result is: Mxssxssxppx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s