Most answers have length() or charAt() or toCharArray() methods used. However I want to do get the position of a substring in a string without using any of these. Thanks in advance for your help
Asked
Active
Viewed 145 times
-3
-
Why would you want to not use those methods? Work easier, not harder. – DanSchneiderNA Oct 27 '14 at 23:28
-
Maybe you should be a bit more specific on what you are actually trying do. Maybe regex is a possibility. – PzYon Oct 27 '14 at 23:28
-
3Would [indexOf()](http://www.tutorialspoint.com/java/java_string_indexof.htm) help? – Bijan Oct 27 '14 at 23:29
-
You can use Pattern Matcher: http://stackoverflow.com/questions/8938498/get-the-index-of-a-pattern-in-a-string-using-regex – Amir Moghimi Oct 27 '14 at 23:37
-
If you do not want to use any inbuilt string methods , pattern matching may be your only option. – Chiseled Oct 27 '14 at 23:44
-
@TylerWeaver My guess would be that it's a homework assignment, and not using those methods are part of the requirements. – Dennis Meng Oct 28 '14 at 04:42
-
@DennisMeng I think it's impossible to achieve what he is asking. Unless he means not using any of the built-in string functions. – DanSchneiderNA Oct 28 '14 at 04:47
-
Yeah, I'm assuming that too (no built-in string methods, not no built-in methods) – Dennis Meng Oct 28 '14 at 04:48
2 Answers
3
That's not possible. A String
is backed by a char[]
. The only way to access that char[]
or its elements is through String
methods. If you can't use any of those methods, you cannot find a substring within it.

Sotirios Delimanolis
- 274,122
- 60
- 696
- 724
-3
public class HelloWorld {
public static void main(String []args) {
char[] string = {'t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', 'g', 0};
char[] substring = {'i', 's', ' ', 0};
int c = 0;
Boolean found = false;
int position = 0;
while (string[c] != '\0') {
int f = 0;
while (string[c+f]==substring[f]) f++;
if (substring[f]=='\0') {
found = true;
position = c;
}
c++;
}
if (found)
System.out.println("found at position " + position);
}
}

CharlieS
- 1,432
- 1
- 9
- 10
-
Well, I suppose *not using Java* is one way to avoid using Java's built-in methods, but what language *is* that? ;) – Alan Moore Oct 28 '14 at 01:08
-
pfft. so i wrote from memory.. only non-java is assigning string to char[]. change assignment to {'t','h','i','s',' ','i','s',' ','a',' ','s','t','r','i','n','g'} and the other string in a similar way, and it will run. Assignments are confused with C – CharlieS Oct 28 '14 at 01:18
-
add a string terminator too ie. {'t','h','i','s',' ','i','s',' ','a',' ','s','t','r','i','n','g',0} – CharlieS Oct 28 '14 at 01:27
-
Yeah, I figured as much. I was poking at you for trying to answer the question in the first place. I suspect it's from a certification exam or something, because I've seen it a few times before (here at SO or elsewhere), and they never say why they're asking. I didn't downvote you though; I saved that for the question itself. – Alan Moore Oct 28 '14 at 04:40
-
i don't care about downvotes, but there are always ways to think outside the box. Telling folk it can't be done and shouldn't be done is not helpful. In testing there are many reasons to write code that don't follow rules and practises.. my answer has a couple of bugs, but it shows you can do anything if you think about it hard enough. I don't suggest using C style coding in Java, but knowing how should not be frowned on :) – CharlieS Oct 28 '14 at 04:57
-
Thanks code Charlie I ran when I run the code I get the following error. – NewUser0907 Oct 28 '14 at 16:06
-
Thanks for the code Charlie, i am getting the following error when i run the program. Can you please suggest Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at StringProblem.getSubsStringPos(StringProblem.java:14) at StringProblem.main(StringProblem.java:31) – NewUser0907 Oct 28 '14 at 16:07
-
ive edited it so it will compile and run.. string to char[] is the only conversion to do exactly what you want. As it is i hope it gives you ideas about character manipulations. The code is compiled and running here: http://www.browxy.com/SavedCode/22195 – CharlieS Oct 28 '14 at 23:14