This question already has answers here:
Java String array: is there a size of method?
(12 answers)
How to get first and last element in an array in java?
(7 answers)
Closed 4 years ago.
There is a string and I split it by whitespace and I want to get the last one.
String test = "Hey Hi Hello"; // Defining "test" String
test = test.split(" ")[2]; // Now "test" is "Hello"
System.out.print(test); // prints Hello
I should do this to get the last word of "test" String. But I know the length of the string. What should I do if I don't know the length of the String? What should I write between [ ] to get the last word?
For example when I get a data from an web page and I don't now the value is what.
test.split returns an array of Strings. Just save it somewhere instead of using it immediately and check its length.
String test = "Hey Hi Hello";
String[] words = test.split(" ");
test = words[words.length - 1];
System.out.print(test);
String[] temp = test.split(" "); //this will split whole string into array using white space.
test = temp[temp.length-1]; //gets the element at the last index of temp array
Related
This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 1 year ago.
I'm wanting to store an a substring into a new array.
Here is my substring data = line.substring(i, i+1);
How do i do this? I have tried String data [] = line.substring(i, i+1) but not worked.
Thanks
.substring() does not return an array, it returns a String so passing it into an array won't work. You can split a String into a String[] array using the .split(String regex) method. The paramter is the delimiting regular expression, meaning if you did "Hello World".split(" "); (with space as the argument), the resulting array would be ["Hello", "World"]. If you want to split all of the characters into their own index of the array, just pass in an empty String "" as the argument of the split method (like line.split("")).
If your goal is to store the String in an array you can pass it in with a regular array declaration and assignment: String[] data = { line.substring(i, i + 1) }; or just make a new array and pass it into the desired index.
This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 5 years ago.
I'm trying to fill an array of 2 elements with a element from another array, split by "." but it doesn't fill (for instance, the input is "83.105" and i need the first element to be "83" and the second "105"). When i try to get the 0 element from numberParts array it shows out of bounds exception. I'm really confused since this method is working on C# but not Java.
String[] inputNumbers = console.nextLine().split(" ");
String[] numberParts = inputNumbers[0].split(".");
System.out.println(numberParts[0]);
Dot (.) is a char that must be escaped in the split method.
why? because is a reserved character in regex
if not, splitting will return an empty array
String myString = "83.105";
String[] x = myString.split("\\.");
System.out.println(x[0]);
System.out.println(x[1]);
This question already has answers here:
Java String split removed empty values
(5 answers)
Closed 6 years ago.
String parts[] = formattedString.split("\\:");
String partA = parts[0];
String partB = parts[1];
I'm trying to split a string before and after the ":" symbol.
For example, "Before:After" works, but in the case where there is nothing after the ":" like "Before: " gives me an exception. I've tried to check for null in parts[1] but it still throws exception.
You need to check the length of the array to determine whether anything at index 1 exists before referencing it.
if(parts.length > 1) {
//do something with it
}
The length of the array that the split method gives is determined by the number of elements that could be splitted.
This means that if you don't have a second element, the length of the array will be 1 instead of 2.
You should check the length of the array to determine if there is a second element.
String parts[] = formattedString.split("\\:");
String partA = parts[0];
String partB = parts[1];
Into above code you want to access index 0 and 1 so please check first the array parts have these index or not. before accessing these index please check the length of array.
This question already has an answer here:
Split string on spaces in Java, except if between quotes (i.e. treat \"hello world\" as one token) [duplicate]
(1 answer)
Closed 8 years ago.
I have a String(freeText) "Manas \"Jaikant IBM\"". I want to split into two strings:
String normalMatch="Manas";
String exactMatch="Jaikant IBM";
It means that String normalMatch contains Manas and String exactMatch contains Jaikant IBM.
i am using the split() method of String class in Java
String[] splittedText= freeText.split("\\s");
I am getting 3 string elements but i need 2 string elements only.
Use substring instead of split:
int index = freeText.indexOf(" ");
String normalMatch = freeText.substring(0,index);
String exactMatch = freeText.substring(index); // endIndex == freeText.length())
Split on quotes(") then, you will get Manas and Jaikant IBM and can ignore the 3rd value.
This question already has answers here:
String.Split with a String?
(2 answers)
Closed 9 years ago.
I have a string "552 s hello"
I want to get the "552" in one substring and "s" in other substring and I want to split this String on the basis of first two spaces.Please help...
You can do it like this:
String t = "522 s hello"
String[] test = t.split(" ");
Now in the String array you will have three values, "522", "s" and "hello. And you can now access each.
Use String#split(String regex):
String[] sp="552 s hello".split(" ");
String fiveFiveTwo=sp[0];
String letterS=sp[1];
The names aren't very useful in the end, so you'll want to rename the strings I've created. You should also catch ArrayIndexOutOfBoundsException.