חיפוש בתוך string

חיפוש בתוך string

איך אפשר בגאווה לחפש string בתוך string ? זאת אומרת שאם יש לי את הstring הבא: abcdefg ואת ה string - def אני אקבל את המיקום של תחילת המחרוזת השניה בתוך הראשונה (3) ?
 
אם אינני טועה אז ב-C

זה ע"י שימוש בפונקציה stsrstr ואין לי שמץ של מושג איך משתמשים בה ב-JAVA ו-JS משתמשים ב:
JAVA: String str = "abcdef"; String str1 = "def"; System.out.println(str.indexOf(str1)); JS: var str="abcdef", str1="def"; alert(str.indexOf(str1));​
 

EndersG

New member
תשובה:

מרטין, הנה התשובה של דוק:
int indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. int indexOf(String str, int fromIndex) Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.​
 
תודה על התשובות ועוד שאלה

בהנחה שבמקום לחפש מחרוזת בתוך מחרוזת אני רוצה לחפש byteArray בתוך byteArray יש דרך מובנת בשפה לעשות את זה ?
 

melatonin

New member
לכל טיפוס primitive בג'אוה

יש מחלקה שעוטפת אותו. במקרה של char מדובר ב Character, והיא כוללת את המתודה הסטטית
String Character.toString(char)​
 
איפה ניתן למצוא את הקוד של indexof

המטרה שלי היא לממש את index of על מערך של ביטים במקום על string בשביל שאוכל לטפל בכל סוג של תו ascii (גם התו 3 שמסיים סוף מחרוזת לדוגמא)
 

justme5

New member
src.zip

ניתן לבחור בעת התקנת ה jdk אם להתקין את הסורסים או לא. ככה זה נראה אצלי:
/** * Returns the index within this string of the first occurrence of the * specified character, starting the search at the specified index. * <p> * If a character with value <code>ch</code> occurs in the character * sequence represented by this <code>String</code> object at an index * no smaller than <code>fromIndex</code>, then the index of the first * such occurrence is returned--that is, the smallest value <i>k</i> * such that: * <blockquote><pre> * (this.charAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex) * </pre></blockquote> * is true. If no such character occurs in this string at or after * position <code>fromIndex</code>, then <code>-1</code> is returned. * <p> * There is no restriction on the value of <code>fromIndex</code>. If it * is negative, it has the same effect as if it were zero: this entire * string may be searched. If it is greater than the length of this * string, it has the same effect as if it were equal to the length of * this string: <code>-1</code> is returned. * * @param ch a character. * @param fromIndex the index to start the search from. * @return the index of the first occurrence of the character in the * character sequence represented by this object that is greater * than or equal to <code>fromIndex</code>, or <code>-1</code> * if the character does not occur. */ public int indexOf(int ch, int fromIndex) { int max = offset + count; char v[] = value; if (fromIndex < 0) { fromIndex = 0; } else if (fromIndex >= count) { // Note: fromIndex might be near -1>>>1. return -1; } for (int i = offset + fromIndex ; i < max ; i++) { if (v == ch) { return i - offset; } } return -1; }
 
למעלה