How to find first character after second dot java










6














Do you have any ideas how could I get first character after second dot of the string.



String str1 = "test.1231.asdasd.cccc.2.a.2";
String str2 = "aaa.1.22224.sadsada";


In first case I should get a and in second 2.
I thought about dividing string with dot, and extracting first character of third element. But it seems to complicated and I think there is better way.










share|improve this question



















  • 5




    Have you tried anything so far ?
    – Arthur Attout
    Aug 24 at 11:09






  • 3




    have you tried regex? which of course isn't less complicated :)
    – Jack Flamp
    Aug 24 at 11:09







  • 1




    Arthur, as I wrote before, splitting. But seems not ok.
    – Suule
    Aug 24 at 11:12















6














Do you have any ideas how could I get first character after second dot of the string.



String str1 = "test.1231.asdasd.cccc.2.a.2";
String str2 = "aaa.1.22224.sadsada";


In first case I should get a and in second 2.
I thought about dividing string with dot, and extracting first character of third element. But it seems to complicated and I think there is better way.










share|improve this question



















  • 5




    Have you tried anything so far ?
    – Arthur Attout
    Aug 24 at 11:09






  • 3




    have you tried regex? which of course isn't less complicated :)
    – Jack Flamp
    Aug 24 at 11:09







  • 1




    Arthur, as I wrote before, splitting. But seems not ok.
    – Suule
    Aug 24 at 11:12













6












6








6


2





Do you have any ideas how could I get first character after second dot of the string.



String str1 = "test.1231.asdasd.cccc.2.a.2";
String str2 = "aaa.1.22224.sadsada";


In first case I should get a and in second 2.
I thought about dividing string with dot, and extracting first character of third element. But it seems to complicated and I think there is better way.










share|improve this question















Do you have any ideas how could I get first character after second dot of the string.



String str1 = "test.1231.asdasd.cccc.2.a.2";
String str2 = "aaa.1.22224.sadsada";


In first case I should get a and in second 2.
I thought about dividing string with dot, and extracting first character of third element. But it seems to complicated and I think there is better way.







java regex string java-8






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 24 at 12:43









deepesh kumar

563414




563414










asked Aug 24 at 11:07









Suule

17715




17715







  • 5




    Have you tried anything so far ?
    – Arthur Attout
    Aug 24 at 11:09






  • 3




    have you tried regex? which of course isn't less complicated :)
    – Jack Flamp
    Aug 24 at 11:09







  • 1




    Arthur, as I wrote before, splitting. But seems not ok.
    – Suule
    Aug 24 at 11:12












  • 5




    Have you tried anything so far ?
    – Arthur Attout
    Aug 24 at 11:09






  • 3




    have you tried regex? which of course isn't less complicated :)
    – Jack Flamp
    Aug 24 at 11:09







  • 1




    Arthur, as I wrote before, splitting. But seems not ok.
    – Suule
    Aug 24 at 11:12







5




5




Have you tried anything so far ?
– Arthur Attout
Aug 24 at 11:09




Have you tried anything so far ?
– Arthur Attout
Aug 24 at 11:09




3




3




have you tried regex? which of course isn't less complicated :)
– Jack Flamp
Aug 24 at 11:09





have you tried regex? which of course isn't less complicated :)
– Jack Flamp
Aug 24 at 11:09





1




1




Arthur, as I wrote before, splitting. But seems not ok.
– Suule
Aug 24 at 11:12




Arthur, as I wrote before, splitting. But seems not ok.
– Suule
Aug 24 at 11:12












7 Answers
7






active

oldest

votes


















10














How about a regex for this?



Pattern p = Pattern.compile(".+?\..+?\.(\w)");
Matcher m = p.matcher(str1);

if (m.find())
System.out.println(m.group(1));



The regex says: find anything one or more times in a non-greedy fashion (.+?), that must be followed by a dot (\.), than again anything one or more times in a non-greedy fashion (.+?) followed by a dot (\.). After this was matched take the first word character in the first group ((\w)).






share|improve this answer


















  • 1




    Can you please explain the regex?
    – Jack Flamp
    Aug 24 at 11:14






  • 6




    The OP did not mandate the desired character to be a word character, so you should use just . instead of w. And you may avoid repeating yourself: Pattern p = Pattern.compile("(?:.+?\.)2(.)");. If you want to extract a char without going through a String, you can also use Matcher m = p.matcher(str1); if(m.find()) char c = str1.charAt(m.start(1)); . Or, if you want a string, you can do it ad-hoc: String s = str1.replaceFirst("(?:.+?\.)2(.).*", "$1");.
    – Holger
    Aug 24 at 12:32











  • .+? -> .*? there's nowhere said that characters are expected between dots
    – loa_in_
    Aug 24 at 15:05










  • @loa_in_ youre probably right, I just took the OPs examples as they were in the question
    – Eugene
    Aug 25 at 8:06


















2














Usually regex will do an excellent work here. Still if you are looking for something more customizable then consider the following implementation:



private static int positionOf(String source, String target, int match) 
if (match < 1)
return -1;

int result = -1;

do
result = source.indexOf(target, result + target.length());
while (--match > 0 && result > 0);

return result;



and then the test is done with:



String str1 = "test..1231.asdasd.cccc..2.a.2.";


System.out.println(positionOf(str1, ".", 3)); -> // prints 10

System.out.println(positionOf(str1, "c", 4)); -> // prints 21

System.out.println(positionOf(str1, "c", 5)); -> // prints -1

System.out.println(positionOf(str1, "..", 2)); -> // prints 22 -> just have in mind that the first symbol after the match is at position 22 + target.length() and also there might be none element with such index in the char array.






share|improve this answer




























    2














    Without using pattern, you can use subString and charAt method of String class to achieve this



    // You can return String instead of char
    public static char returnSecondChar(String strParam)
    String tmpSubString = "";
    // First check if . exists in the string.
    if (strParam.indexOf('.') != -1)
    // If yes, then extract substring starting from .+1
    tmpSubString = strParam.substring(strParam.indexOf('.') + 1);
    System.out.println(tmpSubString);

    // Check if second '.' exists
    if (tmpSubString.indexOf('.') != -1)

    // If it exists, get the char at index of . + 1
    return tmpSubString.charAt(tmpSubString.indexOf('.') + 1);


    // If 2 '.' don't exists in the string, return '-'. Here you can return any thing
    return '-';






    share|improve this answer






















    • No need to use subString since indexOf provides implementation that takes starting index.
      – dbl
      Aug 24 at 11:26










    • I have called substring because I wanted to check if at all . exists of not. If it did, then to call indexof again
      – Ashishkumar Singh
      Aug 24 at 11:34


















    1














    You could do it by splitting the String like this:



    public static void main(String args) 
    String str1 = "test.1231.asdasd.cccc.2.a.2";
    String str2 = "aaa.1.22224.sadsada";

    System.out.println(getCharAfterSecondDot(str1));
    System.out.println(getCharAfterSecondDot(str2));


    public static char getCharAfterSecondDot(String s)
    String split = s.split("\.");
    // TODO check if there are values in the array!
    return split[2].charAt(0);



    I don't think it is too complicated, but using a directly matching regex is a very good (maybe better) solution anyway.



    Please note that there might be the case of a String input with less than two dots, which would have to be handled (see TODO comment in the code).






    share|improve this answer




























      1














      You can use Java Stream API since Java 8:



      String string = "test.1231.asdasd.cccc.2.a.2";
      Arrays.stream(string.split("\.")) // Split by dot
      .skip(2).limit(1) // Skip 2 initial parts and limit to one
      .map(i -> i.substring(0, 1)) // Map to the first character
      .findFirst().ifPresent(System.out::println); // Get first and print if exists


      However, I recommend you to stick with Regex, which is safer and a correct way to do so:




      Here is the Regex you need (demo available at Regex101):



      .*?..*?.(.).*


      Don't forget to escape the special characters with double-slash \.



      String array = new String[3];
      array[0] = "test.1231.asdasd.cccc.2.a.2";
      array[1] = "aaa.1.22224.sadsada";
      array[2] = "test";

      Pattern p = Pattern.compile(".*?\..*?\.(.).*");
      for (int i=0; i<array.length; i++)
      Matcher m = p.matcher(array[i]);
      if (m.find())
      System.out.println(m.group(1));




      This code prints two results on each line: a, 2 and an empty lane because on the 3rd String, there is no match.






      share|improve this answer






















      • Using string.split("\.", 3) makes the limit(1) obsolete. Since the stream has at most one element, you can use forEach(…) instead of .findFirst().ifPresent(…). In the regex, the .* at the end is obsolete.
        – Holger
        Aug 24 at 12:31


















      0














      A plain solution using String.indexOf:



      public static Character getCharAfterSecondDot(String s) 
      int indexOfFirstDot = s.indexOf('.');
      if (!isValidIndex(indexOfFirstDot, s))
      return null;


      int indexOfSecondDot = s.indexOf('.', indexOfFirstDot + 1);
      return isValidIndex(indexOfSecondDot, s) ?
      s.charAt(indexOfSecondDot + 1) :
      null;


      protected static boolean isValidIndex(int index, String s)
      return index != -1 && index < s.length() - 1;



      Using indexOf(int ch) and indexOf(int ch, int fromIndex) needs only to examine all characters in worst case.



      And a second version implementing the same logic using indexOf with Optional:



      public static Character getCharAfterSecondDot(String s) 
      return Optional.of(s.indexOf('.'))
      .filter(i -> isValidIndex(i, s))
      .map(i -> s.indexOf('.', i + 1))
      .filter(i -> isValidIndex(i, s))
      .map(i -> s.charAt(i + 1))
      .orElse(null);






      share|improve this answer






























        0














        Just another approach, not a one-liner code but simple.



        public class Test
        public static void main (String args)
        for(String str:new String"test.1231.asdasd.cccc.2.a.2","aaa.1.22224.sadsada")
        int n = 0;
        for(char c : str.toCharArray())
        if(2 == n)
        System.out.printf("found char: %c%n",c);
        break;

        if('.' == c)
        n ++;







        found char: a

        found char: 2






        share|improve this answer






















        • Question is tagged with [java] :)
          – dbl
          Aug 24 at 12:50









        protected by Community Aug 24 at 12:53



        Thank you for your interest in this question.
        Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



        Would you like to answer one of these unanswered questions instead?














        7 Answers
        7






        active

        oldest

        votes








        7 Answers
        7






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        10














        How about a regex for this?



        Pattern p = Pattern.compile(".+?\..+?\.(\w)");
        Matcher m = p.matcher(str1);

        if (m.find())
        System.out.println(m.group(1));



        The regex says: find anything one or more times in a non-greedy fashion (.+?), that must be followed by a dot (\.), than again anything one or more times in a non-greedy fashion (.+?) followed by a dot (\.). After this was matched take the first word character in the first group ((\w)).






        share|improve this answer


















        • 1




          Can you please explain the regex?
          – Jack Flamp
          Aug 24 at 11:14






        • 6




          The OP did not mandate the desired character to be a word character, so you should use just . instead of w. And you may avoid repeating yourself: Pattern p = Pattern.compile("(?:.+?\.)2(.)");. If you want to extract a char without going through a String, you can also use Matcher m = p.matcher(str1); if(m.find()) char c = str1.charAt(m.start(1)); . Or, if you want a string, you can do it ad-hoc: String s = str1.replaceFirst("(?:.+?\.)2(.).*", "$1");.
          – Holger
          Aug 24 at 12:32











        • .+? -> .*? there's nowhere said that characters are expected between dots
          – loa_in_
          Aug 24 at 15:05










        • @loa_in_ youre probably right, I just took the OPs examples as they were in the question
          – Eugene
          Aug 25 at 8:06















        10














        How about a regex for this?



        Pattern p = Pattern.compile(".+?\..+?\.(\w)");
        Matcher m = p.matcher(str1);

        if (m.find())
        System.out.println(m.group(1));



        The regex says: find anything one or more times in a non-greedy fashion (.+?), that must be followed by a dot (\.), than again anything one or more times in a non-greedy fashion (.+?) followed by a dot (\.). After this was matched take the first word character in the first group ((\w)).






        share|improve this answer


















        • 1




          Can you please explain the regex?
          – Jack Flamp
          Aug 24 at 11:14






        • 6




          The OP did not mandate the desired character to be a word character, so you should use just . instead of w. And you may avoid repeating yourself: Pattern p = Pattern.compile("(?:.+?\.)2(.)");. If you want to extract a char without going through a String, you can also use Matcher m = p.matcher(str1); if(m.find()) char c = str1.charAt(m.start(1)); . Or, if you want a string, you can do it ad-hoc: String s = str1.replaceFirst("(?:.+?\.)2(.).*", "$1");.
          – Holger
          Aug 24 at 12:32











        • .+? -> .*? there's nowhere said that characters are expected between dots
          – loa_in_
          Aug 24 at 15:05










        • @loa_in_ youre probably right, I just took the OPs examples as they were in the question
          – Eugene
          Aug 25 at 8:06













        10












        10








        10






        How about a regex for this?



        Pattern p = Pattern.compile(".+?\..+?\.(\w)");
        Matcher m = p.matcher(str1);

        if (m.find())
        System.out.println(m.group(1));



        The regex says: find anything one or more times in a non-greedy fashion (.+?), that must be followed by a dot (\.), than again anything one or more times in a non-greedy fashion (.+?) followed by a dot (\.). After this was matched take the first word character in the first group ((\w)).






        share|improve this answer














        How about a regex for this?



        Pattern p = Pattern.compile(".+?\..+?\.(\w)");
        Matcher m = p.matcher(str1);

        if (m.find())
        System.out.println(m.group(1));



        The regex says: find anything one or more times in a non-greedy fashion (.+?), that must be followed by a dot (\.), than again anything one or more times in a non-greedy fashion (.+?) followed by a dot (\.). After this was matched take the first word character in the first group ((\w)).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 24 at 11:16

























        answered Aug 24 at 11:11









        Eugene

        68k997160




        68k997160







        • 1




          Can you please explain the regex?
          – Jack Flamp
          Aug 24 at 11:14






        • 6




          The OP did not mandate the desired character to be a word character, so you should use just . instead of w. And you may avoid repeating yourself: Pattern p = Pattern.compile("(?:.+?\.)2(.)");. If you want to extract a char without going through a String, you can also use Matcher m = p.matcher(str1); if(m.find()) char c = str1.charAt(m.start(1)); . Or, if you want a string, you can do it ad-hoc: String s = str1.replaceFirst("(?:.+?\.)2(.).*", "$1");.
          – Holger
          Aug 24 at 12:32











        • .+? -> .*? there's nowhere said that characters are expected between dots
          – loa_in_
          Aug 24 at 15:05










        • @loa_in_ youre probably right, I just took the OPs examples as they were in the question
          – Eugene
          Aug 25 at 8:06












        • 1




          Can you please explain the regex?
          – Jack Flamp
          Aug 24 at 11:14






        • 6




          The OP did not mandate the desired character to be a word character, so you should use just . instead of w. And you may avoid repeating yourself: Pattern p = Pattern.compile("(?:.+?\.)2(.)");. If you want to extract a char without going through a String, you can also use Matcher m = p.matcher(str1); if(m.find()) char c = str1.charAt(m.start(1)); . Or, if you want a string, you can do it ad-hoc: String s = str1.replaceFirst("(?:.+?\.)2(.).*", "$1");.
          – Holger
          Aug 24 at 12:32











        • .+? -> .*? there's nowhere said that characters are expected between dots
          – loa_in_
          Aug 24 at 15:05










        • @loa_in_ youre probably right, I just took the OPs examples as they were in the question
          – Eugene
          Aug 25 at 8:06







        1




        1




        Can you please explain the regex?
        – Jack Flamp
        Aug 24 at 11:14




        Can you please explain the regex?
        – Jack Flamp
        Aug 24 at 11:14




        6




        6




        The OP did not mandate the desired character to be a word character, so you should use just . instead of w. And you may avoid repeating yourself: Pattern p = Pattern.compile("(?:.+?\.)2(.)");. If you want to extract a char without going through a String, you can also use Matcher m = p.matcher(str1); if(m.find()) char c = str1.charAt(m.start(1)); . Or, if you want a string, you can do it ad-hoc: String s = str1.replaceFirst("(?:.+?\.)2(.).*", "$1");.
        – Holger
        Aug 24 at 12:32





        The OP did not mandate the desired character to be a word character, so you should use just . instead of w. And you may avoid repeating yourself: Pattern p = Pattern.compile("(?:.+?\.)2(.)");. If you want to extract a char without going through a String, you can also use Matcher m = p.matcher(str1); if(m.find()) char c = str1.charAt(m.start(1)); . Or, if you want a string, you can do it ad-hoc: String s = str1.replaceFirst("(?:.+?\.)2(.).*", "$1");.
        – Holger
        Aug 24 at 12:32













        .+? -> .*? there's nowhere said that characters are expected between dots
        – loa_in_
        Aug 24 at 15:05




        .+? -> .*? there's nowhere said that characters are expected between dots
        – loa_in_
        Aug 24 at 15:05












        @loa_in_ youre probably right, I just took the OPs examples as they were in the question
        – Eugene
        Aug 25 at 8:06




        @loa_in_ youre probably right, I just took the OPs examples as they were in the question
        – Eugene
        Aug 25 at 8:06













        2














        Usually regex will do an excellent work here. Still if you are looking for something more customizable then consider the following implementation:



        private static int positionOf(String source, String target, int match) 
        if (match < 1)
        return -1;

        int result = -1;

        do
        result = source.indexOf(target, result + target.length());
        while (--match > 0 && result > 0);

        return result;



        and then the test is done with:



        String str1 = "test..1231.asdasd.cccc..2.a.2.";


        System.out.println(positionOf(str1, ".", 3)); -> // prints 10

        System.out.println(positionOf(str1, "c", 4)); -> // prints 21

        System.out.println(positionOf(str1, "c", 5)); -> // prints -1

        System.out.println(positionOf(str1, "..", 2)); -> // prints 22 -> just have in mind that the first symbol after the match is at position 22 + target.length() and also there might be none element with such index in the char array.






        share|improve this answer

























          2














          Usually regex will do an excellent work here. Still if you are looking for something more customizable then consider the following implementation:



          private static int positionOf(String source, String target, int match) 
          if (match < 1)
          return -1;

          int result = -1;

          do
          result = source.indexOf(target, result + target.length());
          while (--match > 0 && result > 0);

          return result;



          and then the test is done with:



          String str1 = "test..1231.asdasd.cccc..2.a.2.";


          System.out.println(positionOf(str1, ".", 3)); -> // prints 10

          System.out.println(positionOf(str1, "c", 4)); -> // prints 21

          System.out.println(positionOf(str1, "c", 5)); -> // prints -1

          System.out.println(positionOf(str1, "..", 2)); -> // prints 22 -> just have in mind that the first symbol after the match is at position 22 + target.length() and also there might be none element with such index in the char array.






          share|improve this answer























            2












            2








            2






            Usually regex will do an excellent work here. Still if you are looking for something more customizable then consider the following implementation:



            private static int positionOf(String source, String target, int match) 
            if (match < 1)
            return -1;

            int result = -1;

            do
            result = source.indexOf(target, result + target.length());
            while (--match > 0 && result > 0);

            return result;



            and then the test is done with:



            String str1 = "test..1231.asdasd.cccc..2.a.2.";


            System.out.println(positionOf(str1, ".", 3)); -> // prints 10

            System.out.println(positionOf(str1, "c", 4)); -> // prints 21

            System.out.println(positionOf(str1, "c", 5)); -> // prints -1

            System.out.println(positionOf(str1, "..", 2)); -> // prints 22 -> just have in mind that the first symbol after the match is at position 22 + target.length() and also there might be none element with such index in the char array.






            share|improve this answer












            Usually regex will do an excellent work here. Still if you are looking for something more customizable then consider the following implementation:



            private static int positionOf(String source, String target, int match) 
            if (match < 1)
            return -1;

            int result = -1;

            do
            result = source.indexOf(target, result + target.length());
            while (--match > 0 && result > 0);

            return result;



            and then the test is done with:



            String str1 = "test..1231.asdasd.cccc..2.a.2.";


            System.out.println(positionOf(str1, ".", 3)); -> // prints 10

            System.out.println(positionOf(str1, "c", 4)); -> // prints 21

            System.out.println(positionOf(str1, "c", 5)); -> // prints -1

            System.out.println(positionOf(str1, "..", 2)); -> // prints 22 -> just have in mind that the first symbol after the match is at position 22 + target.length() and also there might be none element with such index in the char array.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 24 at 11:40









            dbl

            670312




            670312





















                2














                Without using pattern, you can use subString and charAt method of String class to achieve this



                // You can return String instead of char
                public static char returnSecondChar(String strParam)
                String tmpSubString = "";
                // First check if . exists in the string.
                if (strParam.indexOf('.') != -1)
                // If yes, then extract substring starting from .+1
                tmpSubString = strParam.substring(strParam.indexOf('.') + 1);
                System.out.println(tmpSubString);

                // Check if second '.' exists
                if (tmpSubString.indexOf('.') != -1)

                // If it exists, get the char at index of . + 1
                return tmpSubString.charAt(tmpSubString.indexOf('.') + 1);


                // If 2 '.' don't exists in the string, return '-'. Here you can return any thing
                return '-';






                share|improve this answer






















                • No need to use subString since indexOf provides implementation that takes starting index.
                  – dbl
                  Aug 24 at 11:26










                • I have called substring because I wanted to check if at all . exists of not. If it did, then to call indexof again
                  – Ashishkumar Singh
                  Aug 24 at 11:34















                2














                Without using pattern, you can use subString and charAt method of String class to achieve this



                // You can return String instead of char
                public static char returnSecondChar(String strParam)
                String tmpSubString = "";
                // First check if . exists in the string.
                if (strParam.indexOf('.') != -1)
                // If yes, then extract substring starting from .+1
                tmpSubString = strParam.substring(strParam.indexOf('.') + 1);
                System.out.println(tmpSubString);

                // Check if second '.' exists
                if (tmpSubString.indexOf('.') != -1)

                // If it exists, get the char at index of . + 1
                return tmpSubString.charAt(tmpSubString.indexOf('.') + 1);


                // If 2 '.' don't exists in the string, return '-'. Here you can return any thing
                return '-';






                share|improve this answer






















                • No need to use subString since indexOf provides implementation that takes starting index.
                  – dbl
                  Aug 24 at 11:26










                • I have called substring because I wanted to check if at all . exists of not. If it did, then to call indexof again
                  – Ashishkumar Singh
                  Aug 24 at 11:34













                2












                2








                2






                Without using pattern, you can use subString and charAt method of String class to achieve this



                // You can return String instead of char
                public static char returnSecondChar(String strParam)
                String tmpSubString = "";
                // First check if . exists in the string.
                if (strParam.indexOf('.') != -1)
                // If yes, then extract substring starting from .+1
                tmpSubString = strParam.substring(strParam.indexOf('.') + 1);
                System.out.println(tmpSubString);

                // Check if second '.' exists
                if (tmpSubString.indexOf('.') != -1)

                // If it exists, get the char at index of . + 1
                return tmpSubString.charAt(tmpSubString.indexOf('.') + 1);


                // If 2 '.' don't exists in the string, return '-'. Here you can return any thing
                return '-';






                share|improve this answer














                Without using pattern, you can use subString and charAt method of String class to achieve this



                // You can return String instead of char
                public static char returnSecondChar(String strParam)
                String tmpSubString = "";
                // First check if . exists in the string.
                if (strParam.indexOf('.') != -1)
                // If yes, then extract substring starting from .+1
                tmpSubString = strParam.substring(strParam.indexOf('.') + 1);
                System.out.println(tmpSubString);

                // Check if second '.' exists
                if (tmpSubString.indexOf('.') != -1)

                // If it exists, get the char at index of . + 1
                return tmpSubString.charAt(tmpSubString.indexOf('.') + 1);


                // If 2 '.' don't exists in the string, return '-'. Here you can return any thing
                return '-';







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 24 at 11:45

























                answered Aug 24 at 11:16









                Ashishkumar Singh

                1,8371921




                1,8371921











                • No need to use subString since indexOf provides implementation that takes starting index.
                  – dbl
                  Aug 24 at 11:26










                • I have called substring because I wanted to check if at all . exists of not. If it did, then to call indexof again
                  – Ashishkumar Singh
                  Aug 24 at 11:34
















                • No need to use subString since indexOf provides implementation that takes starting index.
                  – dbl
                  Aug 24 at 11:26










                • I have called substring because I wanted to check if at all . exists of not. If it did, then to call indexof again
                  – Ashishkumar Singh
                  Aug 24 at 11:34















                No need to use subString since indexOf provides implementation that takes starting index.
                – dbl
                Aug 24 at 11:26




                No need to use subString since indexOf provides implementation that takes starting index.
                – dbl
                Aug 24 at 11:26












                I have called substring because I wanted to check if at all . exists of not. If it did, then to call indexof again
                – Ashishkumar Singh
                Aug 24 at 11:34




                I have called substring because I wanted to check if at all . exists of not. If it did, then to call indexof again
                – Ashishkumar Singh
                Aug 24 at 11:34











                1














                You could do it by splitting the String like this:



                public static void main(String args) 
                String str1 = "test.1231.asdasd.cccc.2.a.2";
                String str2 = "aaa.1.22224.sadsada";

                System.out.println(getCharAfterSecondDot(str1));
                System.out.println(getCharAfterSecondDot(str2));


                public static char getCharAfterSecondDot(String s)
                String split = s.split("\.");
                // TODO check if there are values in the array!
                return split[2].charAt(0);



                I don't think it is too complicated, but using a directly matching regex is a very good (maybe better) solution anyway.



                Please note that there might be the case of a String input with less than two dots, which would have to be handled (see TODO comment in the code).






                share|improve this answer

























                  1














                  You could do it by splitting the String like this:



                  public static void main(String args) 
                  String str1 = "test.1231.asdasd.cccc.2.a.2";
                  String str2 = "aaa.1.22224.sadsada";

                  System.out.println(getCharAfterSecondDot(str1));
                  System.out.println(getCharAfterSecondDot(str2));


                  public static char getCharAfterSecondDot(String s)
                  String split = s.split("\.");
                  // TODO check if there are values in the array!
                  return split[2].charAt(0);



                  I don't think it is too complicated, but using a directly matching regex is a very good (maybe better) solution anyway.



                  Please note that there might be the case of a String input with less than two dots, which would have to be handled (see TODO comment in the code).






                  share|improve this answer























                    1












                    1








                    1






                    You could do it by splitting the String like this:



                    public static void main(String args) 
                    String str1 = "test.1231.asdasd.cccc.2.a.2";
                    String str2 = "aaa.1.22224.sadsada";

                    System.out.println(getCharAfterSecondDot(str1));
                    System.out.println(getCharAfterSecondDot(str2));


                    public static char getCharAfterSecondDot(String s)
                    String split = s.split("\.");
                    // TODO check if there are values in the array!
                    return split[2].charAt(0);



                    I don't think it is too complicated, but using a directly matching regex is a very good (maybe better) solution anyway.



                    Please note that there might be the case of a String input with less than two dots, which would have to be handled (see TODO comment in the code).






                    share|improve this answer












                    You could do it by splitting the String like this:



                    public static void main(String args) 
                    String str1 = "test.1231.asdasd.cccc.2.a.2";
                    String str2 = "aaa.1.22224.sadsada";

                    System.out.println(getCharAfterSecondDot(str1));
                    System.out.println(getCharAfterSecondDot(str2));


                    public static char getCharAfterSecondDot(String s)
                    String split = s.split("\.");
                    // TODO check if there are values in the array!
                    return split[2].charAt(0);



                    I don't think it is too complicated, but using a directly matching regex is a very good (maybe better) solution anyway.



                    Please note that there might be the case of a String input with less than two dots, which would have to be handled (see TODO comment in the code).







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 24 at 11:24









                    deHaar

                    2,15631527




                    2,15631527





















                        1














                        You can use Java Stream API since Java 8:



                        String string = "test.1231.asdasd.cccc.2.a.2";
                        Arrays.stream(string.split("\.")) // Split by dot
                        .skip(2).limit(1) // Skip 2 initial parts and limit to one
                        .map(i -> i.substring(0, 1)) // Map to the first character
                        .findFirst().ifPresent(System.out::println); // Get first and print if exists


                        However, I recommend you to stick with Regex, which is safer and a correct way to do so:




                        Here is the Regex you need (demo available at Regex101):



                        .*?..*?.(.).*


                        Don't forget to escape the special characters with double-slash \.



                        String array = new String[3];
                        array[0] = "test.1231.asdasd.cccc.2.a.2";
                        array[1] = "aaa.1.22224.sadsada";
                        array[2] = "test";

                        Pattern p = Pattern.compile(".*?\..*?\.(.).*");
                        for (int i=0; i<array.length; i++)
                        Matcher m = p.matcher(array[i]);
                        if (m.find())
                        System.out.println(m.group(1));




                        This code prints two results on each line: a, 2 and an empty lane because on the 3rd String, there is no match.






                        share|improve this answer






















                        • Using string.split("\.", 3) makes the limit(1) obsolete. Since the stream has at most one element, you can use forEach(…) instead of .findFirst().ifPresent(…). In the regex, the .* at the end is obsolete.
                          – Holger
                          Aug 24 at 12:31















                        1














                        You can use Java Stream API since Java 8:



                        String string = "test.1231.asdasd.cccc.2.a.2";
                        Arrays.stream(string.split("\.")) // Split by dot
                        .skip(2).limit(1) // Skip 2 initial parts and limit to one
                        .map(i -> i.substring(0, 1)) // Map to the first character
                        .findFirst().ifPresent(System.out::println); // Get first and print if exists


                        However, I recommend you to stick with Regex, which is safer and a correct way to do so:




                        Here is the Regex you need (demo available at Regex101):



                        .*?..*?.(.).*


                        Don't forget to escape the special characters with double-slash \.



                        String array = new String[3];
                        array[0] = "test.1231.asdasd.cccc.2.a.2";
                        array[1] = "aaa.1.22224.sadsada";
                        array[2] = "test";

                        Pattern p = Pattern.compile(".*?\..*?\.(.).*");
                        for (int i=0; i<array.length; i++)
                        Matcher m = p.matcher(array[i]);
                        if (m.find())
                        System.out.println(m.group(1));




                        This code prints two results on each line: a, 2 and an empty lane because on the 3rd String, there is no match.






                        share|improve this answer






















                        • Using string.split("\.", 3) makes the limit(1) obsolete. Since the stream has at most one element, you can use forEach(…) instead of .findFirst().ifPresent(…). In the regex, the .* at the end is obsolete.
                          – Holger
                          Aug 24 at 12:31













                        1












                        1








                        1






                        You can use Java Stream API since Java 8:



                        String string = "test.1231.asdasd.cccc.2.a.2";
                        Arrays.stream(string.split("\.")) // Split by dot
                        .skip(2).limit(1) // Skip 2 initial parts and limit to one
                        .map(i -> i.substring(0, 1)) // Map to the first character
                        .findFirst().ifPresent(System.out::println); // Get first and print if exists


                        However, I recommend you to stick with Regex, which is safer and a correct way to do so:




                        Here is the Regex you need (demo available at Regex101):



                        .*?..*?.(.).*


                        Don't forget to escape the special characters with double-slash \.



                        String array = new String[3];
                        array[0] = "test.1231.asdasd.cccc.2.a.2";
                        array[1] = "aaa.1.22224.sadsada";
                        array[2] = "test";

                        Pattern p = Pattern.compile(".*?\..*?\.(.).*");
                        for (int i=0; i<array.length; i++)
                        Matcher m = p.matcher(array[i]);
                        if (m.find())
                        System.out.println(m.group(1));




                        This code prints two results on each line: a, 2 and an empty lane because on the 3rd String, there is no match.






                        share|improve this answer














                        You can use Java Stream API since Java 8:



                        String string = "test.1231.asdasd.cccc.2.a.2";
                        Arrays.stream(string.split("\.")) // Split by dot
                        .skip(2).limit(1) // Skip 2 initial parts and limit to one
                        .map(i -> i.substring(0, 1)) // Map to the first character
                        .findFirst().ifPresent(System.out::println); // Get first and print if exists


                        However, I recommend you to stick with Regex, which is safer and a correct way to do so:




                        Here is the Regex you need (demo available at Regex101):



                        .*?..*?.(.).*


                        Don't forget to escape the special characters with double-slash \.



                        String array = new String[3];
                        array[0] = "test.1231.asdasd.cccc.2.a.2";
                        array[1] = "aaa.1.22224.sadsada";
                        array[2] = "test";

                        Pattern p = Pattern.compile(".*?\..*?\.(.).*");
                        for (int i=0; i<array.length; i++)
                        Matcher m = p.matcher(array[i]);
                        if (m.find())
                        System.out.println(m.group(1));




                        This code prints two results on each line: a, 2 and an empty lane because on the 3rd String, there is no match.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Aug 24 at 11:34

























                        answered Aug 24 at 11:28









                        Nikolas

                        12.7k53263




                        12.7k53263











                        • Using string.split("\.", 3) makes the limit(1) obsolete. Since the stream has at most one element, you can use forEach(…) instead of .findFirst().ifPresent(…). In the regex, the .* at the end is obsolete.
                          – Holger
                          Aug 24 at 12:31
















                        • Using string.split("\.", 3) makes the limit(1) obsolete. Since the stream has at most one element, you can use forEach(…) instead of .findFirst().ifPresent(…). In the regex, the .* at the end is obsolete.
                          – Holger
                          Aug 24 at 12:31















                        Using string.split("\.", 3) makes the limit(1) obsolete. Since the stream has at most one element, you can use forEach(…) instead of .findFirst().ifPresent(…). In the regex, the .* at the end is obsolete.
                        – Holger
                        Aug 24 at 12:31




                        Using string.split("\.", 3) makes the limit(1) obsolete. Since the stream has at most one element, you can use forEach(…) instead of .findFirst().ifPresent(…). In the regex, the .* at the end is obsolete.
                        – Holger
                        Aug 24 at 12:31











                        0














                        A plain solution using String.indexOf:



                        public static Character getCharAfterSecondDot(String s) 
                        int indexOfFirstDot = s.indexOf('.');
                        if (!isValidIndex(indexOfFirstDot, s))
                        return null;


                        int indexOfSecondDot = s.indexOf('.', indexOfFirstDot + 1);
                        return isValidIndex(indexOfSecondDot, s) ?
                        s.charAt(indexOfSecondDot + 1) :
                        null;


                        protected static boolean isValidIndex(int index, String s)
                        return index != -1 && index < s.length() - 1;



                        Using indexOf(int ch) and indexOf(int ch, int fromIndex) needs only to examine all characters in worst case.



                        And a second version implementing the same logic using indexOf with Optional:



                        public static Character getCharAfterSecondDot(String s) 
                        return Optional.of(s.indexOf('.'))
                        .filter(i -> isValidIndex(i, s))
                        .map(i -> s.indexOf('.', i + 1))
                        .filter(i -> isValidIndex(i, s))
                        .map(i -> s.charAt(i + 1))
                        .orElse(null);






                        share|improve this answer



























                          0














                          A plain solution using String.indexOf:



                          public static Character getCharAfterSecondDot(String s) 
                          int indexOfFirstDot = s.indexOf('.');
                          if (!isValidIndex(indexOfFirstDot, s))
                          return null;


                          int indexOfSecondDot = s.indexOf('.', indexOfFirstDot + 1);
                          return isValidIndex(indexOfSecondDot, s) ?
                          s.charAt(indexOfSecondDot + 1) :
                          null;


                          protected static boolean isValidIndex(int index, String s)
                          return index != -1 && index < s.length() - 1;



                          Using indexOf(int ch) and indexOf(int ch, int fromIndex) needs only to examine all characters in worst case.



                          And a second version implementing the same logic using indexOf with Optional:



                          public static Character getCharAfterSecondDot(String s) 
                          return Optional.of(s.indexOf('.'))
                          .filter(i -> isValidIndex(i, s))
                          .map(i -> s.indexOf('.', i + 1))
                          .filter(i -> isValidIndex(i, s))
                          .map(i -> s.charAt(i + 1))
                          .orElse(null);






                          share|improve this answer

























                            0












                            0








                            0






                            A plain solution using String.indexOf:



                            public static Character getCharAfterSecondDot(String s) 
                            int indexOfFirstDot = s.indexOf('.');
                            if (!isValidIndex(indexOfFirstDot, s))
                            return null;


                            int indexOfSecondDot = s.indexOf('.', indexOfFirstDot + 1);
                            return isValidIndex(indexOfSecondDot, s) ?
                            s.charAt(indexOfSecondDot + 1) :
                            null;


                            protected static boolean isValidIndex(int index, String s)
                            return index != -1 && index < s.length() - 1;



                            Using indexOf(int ch) and indexOf(int ch, int fromIndex) needs only to examine all characters in worst case.



                            And a second version implementing the same logic using indexOf with Optional:



                            public static Character getCharAfterSecondDot(String s) 
                            return Optional.of(s.indexOf('.'))
                            .filter(i -> isValidIndex(i, s))
                            .map(i -> s.indexOf('.', i + 1))
                            .filter(i -> isValidIndex(i, s))
                            .map(i -> s.charAt(i + 1))
                            .orElse(null);






                            share|improve this answer














                            A plain solution using String.indexOf:



                            public static Character getCharAfterSecondDot(String s) 
                            int indexOfFirstDot = s.indexOf('.');
                            if (!isValidIndex(indexOfFirstDot, s))
                            return null;


                            int indexOfSecondDot = s.indexOf('.', indexOfFirstDot + 1);
                            return isValidIndex(indexOfSecondDot, s) ?
                            s.charAt(indexOfSecondDot + 1) :
                            null;


                            protected static boolean isValidIndex(int index, String s)
                            return index != -1 && index < s.length() - 1;



                            Using indexOf(int ch) and indexOf(int ch, int fromIndex) needs only to examine all characters in worst case.



                            And a second version implementing the same logic using indexOf with Optional:



                            public static Character getCharAfterSecondDot(String s) 
                            return Optional.of(s.indexOf('.'))
                            .filter(i -> isValidIndex(i, s))
                            .map(i -> s.indexOf('.', i + 1))
                            .filter(i -> isValidIndex(i, s))
                            .map(i -> s.charAt(i + 1))
                            .orElse(null);







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Aug 24 at 11:52

























                            answered Aug 24 at 11:25









                            LuCio

                            2,7291823




                            2,7291823





















                                0














                                Just another approach, not a one-liner code but simple.



                                public class Test
                                public static void main (String args)
                                for(String str:new String"test.1231.asdasd.cccc.2.a.2","aaa.1.22224.sadsada")
                                int n = 0;
                                for(char c : str.toCharArray())
                                if(2 == n)
                                System.out.printf("found char: %c%n",c);
                                break;

                                if('.' == c)
                                n ++;







                                found char: a

                                found char: 2






                                share|improve this answer






















                                • Question is tagged with [java] :)
                                  – dbl
                                  Aug 24 at 12:50















                                0














                                Just another approach, not a one-liner code but simple.



                                public class Test
                                public static void main (String args)
                                for(String str:new String"test.1231.asdasd.cccc.2.a.2","aaa.1.22224.sadsada")
                                int n = 0;
                                for(char c : str.toCharArray())
                                if(2 == n)
                                System.out.printf("found char: %c%n",c);
                                break;

                                if('.' == c)
                                n ++;







                                found char: a

                                found char: 2






                                share|improve this answer






















                                • Question is tagged with [java] :)
                                  – dbl
                                  Aug 24 at 12:50













                                0












                                0








                                0






                                Just another approach, not a one-liner code but simple.



                                public class Test
                                public static void main (String args)
                                for(String str:new String"test.1231.asdasd.cccc.2.a.2","aaa.1.22224.sadsada")
                                int n = 0;
                                for(char c : str.toCharArray())
                                if(2 == n)
                                System.out.printf("found char: %c%n",c);
                                break;

                                if('.' == c)
                                n ++;







                                found char: a

                                found char: 2






                                share|improve this answer














                                Just another approach, not a one-liner code but simple.



                                public class Test
                                public static void main (String args)
                                for(String str:new String"test.1231.asdasd.cccc.2.a.2","aaa.1.22224.sadsada")
                                int n = 0;
                                for(char c : str.toCharArray())
                                if(2 == n)
                                System.out.printf("found char: %c%n",c);
                                break;

                                if('.' == c)
                                n ++;







                                found char: a

                                found char: 2







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Aug 24 at 23:08

























                                answered Aug 24 at 11:53







                                user2575725


















                                • Question is tagged with [java] :)
                                  – dbl
                                  Aug 24 at 12:50
















                                • Question is tagged with [java] :)
                                  – dbl
                                  Aug 24 at 12:50















                                Question is tagged with [java] :)
                                – dbl
                                Aug 24 at 12:50




                                Question is tagged with [java] :)
                                – dbl
                                Aug 24 at 12:50





                                protected by Community Aug 24 at 12:53



                                Thank you for your interest in this question.
                                Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                Would you like to answer one of these unanswered questions instead?



                                Popular posts from this blog

                                𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                                Crossroads (UK TV series)

                                ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế