Reading Up to a Space in a String Java
The Cord
class has a number of methods for examining the contents of strings, finding characters or substrings within a cord, changing case, and other tasks.
Getting Characters and Substrings past Index
You can become the character at a particular alphabetize inside a cord by invoking the charAt()
accessor method. The alphabetize of the first character is 0, while the alphabetize of the terminal character is length()-1
. For example, the following lawmaking gets the graphic symbol at index 9 in a cord:
String anotherPalindrome = "Niagara. O roar again!"; char aChar = anotherPalindrome.charAt(9);
Indices begin at 0, so the graphic symbol at index 9 is 'O', as illustrated in the following figure:

If you want to get more one consecutive grapheme from a string, you tin use the substring
method. The substring
method has two versions, equally shown in the following table:
Method | Description |
---|---|
String substring(int beginIndex, int endIndex) | Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1 . |
String substring(int beginIndex) | Returns a new cord that is a substring of this string. The integer argument specifies the index of the first grapheme. Hither, the returned substring extends to the end of the original string. |
The following code gets from the Niagara palindrome the substring that extends from index 11 up to, but non including, index 15, which is the discussion "roar":
String anotherPalindrome = "Niagara. O roar again!"; String roar = anotherPalindrome.substring(11, xv);

Other Methods for Manipulating Strings
Hither are several other String
methods for manipulating strings:
Method | Description |
---|---|
Cord[] split up(String regex) String[] split up(String regex, int limit) | Searches for a match as specified by the string argument (which contains a regular expression) and splits this cord into an array of strings appropriately. The optional integer argument specifies the maximum size of the returned array. Regular expressions are covered in the lesson titled "Regular Expressions." |
CharSequence subSequence(int beginIndex, int endIndex) | Returns a new character sequence synthetic from beginIndex index up until endIndex - 1. |
Cord trim() | Returns a re-create of this string with leading and trailing white space removed. |
String toLowerCase() | Returns a copy of this string converted to lowercase or uppercase. If no conversions are necessary, these methods return the original cord. |
Searching for Characters and Substrings in a String
Here are some other Cord
methods for finding characters or substrings within a string. The String
course provides accessor methods that return the position within the string of a specific character or substring: indexOf()
and lastIndexOf()
. The indexOf()
methods search forward from the start of the string, and the lastIndexOf()
methods search backward from the finish of the cord. If a graphic symbol or substring is non constitute, indexOf()
and lastIndexOf()
render -1.
The String
class also provides a search method, contains
, that returns true if the string contains a particular character sequence. Utilise this method when you only need to know that the string contains a graphic symbol sequence, but the precise location isn't of import.
The following table describes the various cord search methods.
Method | Description |
---|---|
int indexOf(int ch) | Returns the index of the first (final) occurrence of the specified character. |
int indexOf(int ch, int fromIndex) | Returns the index of the first (terminal) occurrence of the specified graphic symbol, searching forward (backward) from the specified index. |
int indexOf(Cord str) | Returns the index of the get-go (concluding) occurrence of the specified substring. |
int indexOf(Cord str, int fromIndex) | Returns the index of the starting time (final) occurrence of the specified substring, searching frontward (backward) from the specified index. |
boolean contains(CharSequence s) | Returns true if the string contains the specified character sequence. |
Annotation:CharSequence
is an interface that is implemented by the String
class. Therefore, you can use a string as an argument for the contains()
method.
Replacing Characters and Substrings into a Cord
The String
class has very few methods for inserting characters or substrings into a cord. In general, they are not needed: You can create a new string by concatenation of substrings yous have removed from a cord with the substring that you want to insert.
The Cord
class does take four methods for replacing found characters or substrings, notwithstanding. They are:
Method | Clarification |
---|---|
String supercede(char oldChar, char newChar) | Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar. |
String supervene upon(CharSequence target, CharSequence replacement) | Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. |
Cord replaceAll(String regex, String replacement) | Replaces each substring of this cord that matches the given regular expression with the given replacement. |
String replaceFirst(String regex, Cord replacement) | Replaces the showtime substring of this string that matches the given regular expression with the given replacement. |
An Case
The following class, Filename
, illustrates the utilize of lastIndexOf()
and substring()
to isolate unlike parts of a file name.
Note: The methods in the following Filename
grade don't do any mistake checking and assume that their argument contains a total directory path and a filename with an extension. If these methods were production code, they would verify that their arguments were properly constructed.
public grade Filename { individual String fullPath; private char pathSeparator, extensionSeparator; public Filename(String str, char sep, char ext) { fullPath = str; pathSeparator = sep; extensionSeparator = ext; } public String extension() { int dot = fullPath.lastIndexOf(extensionSeparator); return fullPath.substring(dot + 1); } // gets filename without extension public String filename() { int dot = fullPath.lastIndexOf(extensionSeparator); int sep = fullPath.lastIndexOf(pathSeparator); return fullPath.substring(sep + 1, dot); } public String path() { int sep = fullPath.lastIndexOf(pathSeparator); return fullPath.substring(0, sep); } }
Here is a program, FilenameDemo
, that constructs a Filename
object and calls all of its methods:
public class FilenameDemo { public static void chief(String[] args) { final Cord FPATH = "/home/user/alphabetize.html"; Filename myHomePage = new Filename(FPATH, '/', '.'); Arrangement.out.println("Extension = " + myHomePage.extension()); System.out.println("Filename = " + myHomePage.filename()); System.out.println("Path = " + myHomePage.path()); } }
And here'due south the output from the program:
Extension = html Filename = index Path = /abode/user
As shown in the following effigy, our extension
method uses lastIndexOf
to locate the last occurrence of the period (.) in the file name. Then substring
uses the return value of lastIndexOf
to extract the file proper name extension — that is, the substring from the period to the end of the string. This code assumes that the file name has a period in it; if the file name does not accept a menstruation, lastIndexOf
returns -ane, and the substring method throws a StringIndexOutOfBoundsException
.

Besides, notice that the extension
method uses dot + 1
as the argument to substring
. If the period character (.) is the last graphic symbol of the cord, dot + 1
is equal to the length of the cord, which is one larger than the largest index into the string (because indices commencement at 0). This is a legal argument to substring
because that method accepts an index equal to, simply not greater than, the length of the string and interprets it to mean "the stop of the string."
johnsonniguened1968.blogspot.com
Source: https://docs.oracle.com/javase/tutorial/java/data/manipstrings.html
Kommentar veröffentlichen for "Reading Up to a Space in a String Java"