[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

How to get the next occurence of a character in Java?

If I have a string such as abc.def.ghi.jkl.mno

I want to be able to get substrings from the first occurence of  "." to the next occurence of "."
0
akashsuresh
Asked:
akashsuresh
  • 3
2 Solutions
 
krakatoaCommented:
Use StringTokenizer.
0
 
CEHJCommented:
In one line
s = s.replaceAll(".*?\\.([^.]+).*", "$1");

Open in new window

or
s = s.split("\\.")[1];// poss less efficient

Open in new window


else use charAt and substring
0
 
akashsureshAuthor Commented:
Could you explain how to do it with charAt and substring?
0
 
CEHJCommented:
int dotPos = s.indexOf('.');
if (dotPos >= 0) {
   String temp = s.substring(dotPos);
    dotPos = temp.indexOf('.');
    if (dotPos >= 0) {
       s = temp.substring(dotPos);
    }
}

Open in new window


Now you know why i like it better with regex ;)
0
 
CEHJCommented:
:)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now