Reputation: 131
I have many Strings, for example Animal.dog , World.game , and I want to take only the string before the full stop. e.g I need Animal and World
I don't want to use indexOf() method,or search for a substring since some names are the same e.t.c
Is there any alternative way to achieve that? e.g copy the string until the full stop.
Since the length of the string before the full stop is not constant each time.
Upvotes: 1
Views: 1115
Reputation: 2184
http://www.java-examples.com/java-string-split-example
You just need to split the string using the dot as the separator.
String.split
returns an array of all the resulting substrings, so:
String str = "Animal.dog";
String[] split_str = str.split("\\.");
split_str will be:
["Animal", "dog"]
Upvotes: 1
Reputation: 5516
If there is always going to be one period in the string, you can split the string with period as delimiter and take the first string from the array.
Upvotes: 0
Reputation: 240928
Use split()
, (it probably internally uses indexOf()
)
String partBeforeFullStop = string.split("\\.")[0];
Upvotes: 4