سلام. روزت به خیر. دوست عزیز متد () indexOf برای پیدا کردن ایندکس( index ) یک کاراکتر خاص یا زیر رشته ای ( substring ) از یک رشته(String) مشخص در جاوا کاربرد دا
۱ دقیقه مطالعهآموزش برنامه نویسی »آموزش جاوا و انجام پروژه های برنامه نویسی
محتوای منتخب جاواپرو
خلاصه مطلب
سلام. روزت به خیر. دوست عزیز متد () indexOf برای پیدا کردن ایندکس( index ) یک کاراکتر خاص یا زیر رشته ای ( substring ) از یک رشته(String) مشخص در جاوا کاربرد دا
package javalike;public class Test4 { public static void main(String[] args) { String a = "java like free java"; String str1 = "java"; System.out.println(a.indexOf(str1, 3)); } }
15
System.out.println(a.indexOf(str1, 3));
package javalike;public class indexOfExample { public static void main(String args[]) { String str1 = new String("This is a javalike tutorial"); String str2 = new String("java"); String str3 = new String("like"); String str4 = new String("likes"); System.out.println("index of j in str1: " + str1.indexOf('j')); System.out.println("index of j in str1 after 10th char:" + str1.indexOf('i', 10)); System.out.println("index of j in str1 after 30th char:" + str1.indexOf('j', 30)); System.out .println("index of string str2 in str1:" + str1.indexOf(str2)); System.out.println("index of str2 after 15th char:" + str1.indexOf(str2, 15)); System.out.println("index of string str3:" + str1.indexOf(str3)); System.out.println("index of string str4:" + str1.indexOf(str4)); System.out.println("index of harcoded string:" + str1.indexOf("is")); System.out.println("index of hardcoded string after 4th char:" + str1.indexOf("is", 4)); } }
index of j in str1: 10 index of j in str1 after 10th char:15 index of j in str1 after 30th char:-1 index of string str2 in str1:10 index of str2 after 15th char:-1 index of string str3:14 index of string str4:-1 index of harcoded string:2 index of hardcoded string after 4th char:5