Videos
There's a builtin method find on string objects.
s = "Happy Birthday"
s2 = "py"
print(s.find(s2))
Python is a "batteries included language" there's code written to do most of what you want already (whatever you want).. unless this is homework :)
find returns -1 if the string cannot be found.
Ideally you would use str.find or str.index like demented hedgehog said. But you said you can't ...
Your problem is your code searches only for the first character of your search string which(the first one) is at index 2.
You are basically saying if char[0] is in s, increment index until ch == char[0] which returned 3 when I tested it but it was still wrong. Here's a way to do it.
def find_str(s, char):
index = 0
if char in s:
c = char[0]
for ch in s:
if ch == c:
if s[index:index+len(char)] == char:
return index
index += 1
return -1
print(find_str("Happy birthday", "py"))
print(find_str("Happy birthday", "rth"))
print(find_str("Happy birthday", "rh"))
It produced the following output:
3
8
-1
You can get away without the index variable as you're reassigning it at each step of your loop anyway.
public static int subStringIndex(String str, String substr) {
int substrlen = substr.length();
int strlen = str.length();
int j = 0;
if (substrlen >= 1) {
for (int i = 0; i < strlen; i++) { // iterate through main string
if (str.charAt(i) == substr.charAt(j)) { // check substring
j++; // iterate
if (j == substrlen) { // when to stop
return i - substrlen; //found substring. As i is currently at the end of our substr so sub substrlen
}
}
else {
j = 0;
}
}
}
return -1;
}
Just checking that you intend to be reinventing-the-wheel, you can do:
System.out.println("happy".indexOf("app"));
You did know that, right?
Or, if you want to reformat the 'signature' to match yours, it is:
public static int subStringIndex(String str, String substr) {
return str.indexOf(substr);
}
There are a number of helper methods on String which will help:
- String.indexOf(substr) - return the index of the first occurrence of substr
- String.indexOf(substr, start) - return the index of the first occurrence of substr on or after the start position.
- String.lastIndexOf(substr) - return the index of the last occurrence of substr
- String.lastIndexOf(substr, start) - return the index of the last occurrence of substr starting before the start position. -
You can use a simple regular expression for that:
var matches = Regex.Matches(s, "_+");
var result = new List<int>();
foreach(Match m in matches)
{
result.Add(m.Index);
result.Add(m.Index + m.Length - 1);
}
Console.WriteLine(String.Join(", ", result));
Working example: https://dotnetfiddle.net/GX9MXR
If you want to avoid underscored within words you can also use @"\b_+\b".
This seems to do what you want, if you're averse to regular expressions:
public List<int> GetUnderscorePositions(string source)
{
List<int> positions = new List<int>();
bool withinUnderscore = false;
for (int i = 0; i < source.Length; i++) {
var c = source[i];
if (c == '_') {
if (withinUnderscore) {
continue;
}
else {
withinUnderscore = true;
positions.Add(i);
}
}
else if (withinUnderscore) {
withinUnderscore = false;
positions.Add(i - 1);
}
}
return positions;
}