Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The string class has many methods that you can use to manipulate and parse its values; I'll show you some of these methods in this project, Parsing Strings. I'm starting in an empty main method and I'll declare a variable named s1 with a value of "Welcome to California!" First, I'll show you how to find out how long a string is. I use System.out.println and I'll output "Length of string:" and then to find out how long the string is, use the length method, s1.length.
I'll run the application and there's the result. You can also parse strings using methods of the string class. Here, I'm going to show you how to find a string within a string, I'll declare an integer variable named pos for position, and I'll call the method s1.IndexOf. You'll see when I press Ctrl+Space that there are a number of versions of the IndexOf method; you can parse in a character or string starting from a particular location. I'm going to use the version that's looks for a particular strength, and I'll pass in a literal string of ("California ") and then I'll output the value of the position character using (" Position of California":) and then I'll append pos.
I'll run the application and it tells me that California starts at position 11. Next, I'll show you how to extract a value from a particular position of the string. I'll declare a new string called sub, and I'll call s1.substring. The substring method has three different versions, the one that passes in a single integer, the one that takes a single integer and an ending integer, and one called subsequence, named differently, that returns a character sequence. I'm going to use the substring method and I'll ask for the value starting at position 11, then I'll use System.out .println again, and I'll output the substring that I extracted.
I'll Run the application and there is a result, I get everything starting at the word California! including the exclamation mark at the end. Finally, I'll show you how to trim a string that might have spaces at the end. I'll create a new string called s2 and I'll give it a value of "Welcome!" and I'm going to add a whole bunch spaces at the end of the string. Then I'll declare an int variable named len1, and get its value from s2.length, and I'll output the value of len1.
I'll Run the application and I get a value of 15. Then I'll declare another variable named s3, and I'll get its value from s2.trim, the trim method removes the extra white space. Then I'll make a copy of my print line command and this time I use a slightly more extended syntax, so I don't have to create another integer variable. It will look like this, s3.length. I'll Save my might changes and Run the application and after trimming, I'm down to just eight characters. In this exercise I've shown you some of what I think are the most useful members of the string class, but again, I encourage you to look at the documentation for the string class.
You'll find in this list of methods many, many tools that you can use to extract, manipulate, convert and otherwise use the string class however you need to in your applications.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98382 Viewers
61 Video lessons · 85669 Viewers
71 Video lessons · 69603 Viewers
56 Video lessons · 101928 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.