110 Show
New! Save questions or answers and organize your favorite content. Similar to a question posted here, am looking for a solution in Java. That is, how to find the index of nth occurrence of a character/string from a string? Example: "/folder1/folder2/folder3/". In this case, if I ask for 3rd occurrence of slash (/), it appears before folder3, and I expect to return this index position. My actual intention is to substring it from nth occurrence of a character. Is there any convenient/ready-to-use method available in Java API or do we need to write a small logic on our own to solve this? Also,
asked Oct 20, 2010 at 9:57
2 If your project already depends on Apache Commons you can use
This post has been rewritten as an article here. answered Oct 20, 2010 at 10:02
aioobeaioobe 404k107 gold badges797 silver badges817 bronze badges 2 I believe the easiest solution for finding the Nth occurrence of a String is to use StringUtils.ordinalIndexOf() from Apache Commons. Example:
answered Jul 31, 2012 at 20:55
Al BelskyAl Belsky 1,53613 silver badges15 bronze badges Two simple options occur:
For example:
That may well not perform as well as using
answered Oct 20, 2010 at 10:01
Jon SkeetJon Skeet 1.4m836 gold badges8988 silver badges9094 bronze badges You can try something like this:
Note that I did some assumptions in the regex:
As requested in a comment, I'll try to explain the regex:
This
way you have only to get the substring that matches the 2nd group: Image courtesy by Debuggex answered Oct 20, 2010 at 10:25
andcozandcoz 2,19214 silver badges23 bronze badges 3 I made a few changes to aioobe's answer and got a nth lastIndexOf version, and fix some NPE problems. See code below:
answered Oct 24, 2012 at 8:33
GoofyGoofy 4014 silver badges7 bronze badges 1
Match anything followed by / two times, then again. The third one is the one you want The Matcher state can be used to tell where the last / is answered Oct 20, 2010 at 10:02
2 May be you could achieve this through String.split(..) method also.
answered Jan 12, 2018 at 15:59
MuraliMurali 871 silver badge8 bronze badges Nowadays there IS support of Apache Commons Lang's StringUtils, This is the primitive:
for your problem you can code the following: You can also find the last nth occurrence of a character in a string with the lastOrdinalIndexOf method. answered Jan 24, 2014 at 15:20
ChexpirChexpir 1,85418 silver badges33 bronze badges
answered Oct 20, 2010 at 10:21
SaulSaul 17.8k8 gold badges61 silver badges87 bronze badges Another approach:
answered Oct 21, 2010 at 4:16
This answer improves on @aioobe 's answer. Two bugs in that answer were fixed. Try this !
answered Aug 5, 2015 at 20:07
ARKARK 3,2022 gold badges26 silver badges29 bronze badges My solution:
answered Sep 16, 2015 at 12:43
answered Sep 9, 2014 at 14:24
0 APACHE IMPLEMENTATION (copy-paste: don't import a whole library for one function!)Here is the exact Apache Commons implementation decoupled from their StringUtils library (so that you can just copy paste this and don't have to add a dependency for the library for just one function):
answered Mar 14, 2021 at 22:12
SamSam 2112 silver badges11 bronze badges
Bo Persson 89.1k31 gold badges142 silver badges200 bronze badges answered Mar 20, 2013 at 19:03
The code returns the nth occurrence positions substring aka field width. Example. if string "Stack overflow in low melow" is the string to search 2nd occurance of token "low", you will agree with me that it 2nd occurance is at subtring "18 and 21". indexOfOccurance("Stack overflow in low melow", low, 2) returns 18 and 21 in a string.
answered Jan 27, 2016 at 21:58 0
answered Mar 29, 2019 at 17:51
1 //scala // throw's -1 if the value isn't present for nth time, even if it is present till n-1 th time. // throw's index if the value is present for nth time
answered Sep 8, 2020 at 10:55
Raptor0009Raptor0009 2184 silver badges13 bronze badges It looks like the string you want to substring is a file path. Can't you just split by
answered Jun 24, 2021 at 6:56
answered Aug 10, 2021 at 4:28
sahlaystasahlaysta 211 silver badge3 bronze badges Yes, Regular Expressions definetly help in this regard! To get a substring of everything after the nth occurrence, use this simple one-liner:
For anyone who actually wants the index of the nth occurance, you can use this:
answered Mar 27 at 15:53
FireFuro99FireFuro99 2524 silver badges13 bronze badges Which of the following method call gives the position of the first occurrence of x in the string?The Java String class indexOf() method returns the position of the first occurrence of the specified character or string in a specified string.
Which of the following method call gives the position of the first occurrence of x in the string 51?The charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.
Which method returns true if s1 equals to s2?equals(s2) is a boolean-valued function that returns true if s1 consists of exactly the same sequence of characters as s2.
What is use of charAt () method Mcq?Explanation: charAt() is a method of class String which gives the character specified by the index.
|