Java中String indexOf() 方法解读 2021-12-07 12:13 570阅读 0赞 翻译自[https://www.geeksforgeeks.org/java-string-indexof/][https_www.geeksforgeeks.org_java-string-indexof] **1 String indexOf(char c)**:此方法返回**c字符在string中的下标**【不存在则返回-1】 解释代码: // Java code to demonstrate the working // of String indexOf() public class Index1 { public static void main(String args[]) { // Initialising String String gfg = new String("Welcome to geeksforgeeks"); System.out.print("Found g first at position : "); // Initial index of 'g' will print // prints 11 System.out.println(gfg.indexOf('g')); } } 输出: Found g first at position : 11 **2 String indexOf(char c, int strt)**:此方法**从**下标**strt处开始查找c字符在string中的下标**【不存在则返回-1】 解释代码: // Java code to demonstrate the working // of String indexOf(char ch, int strt) public class Index2 { public static void main(String args[]) { // Initialising String String gfg = new String("Welcome to geeksforgeeks"); System.out.print("Found g after 13th index at position : "); // 2nd index of 'g' will print // prints 19 System.out.println(gfg.indexOf('g', 13)); } } 输出: Found g after 13th index at position : 19 **3 String indexOf(String str)**:此方法返回**字符串在string中的下标**【不存在则返回-1】 解释代码: // Java code to demonstrate the working // of String indexOf(String str) public class Index3 { public static void main(String args[]) { // Initialising string String Str = new String("Welcome to geeksforgeeks"); // Initialising search string String subst = new String("geeks"); // print the index of initial character // of Substring // prints 11 System.out.print("Found geeks starting at position : "); System.out.print(Str.indexOf(subst)); } } 输出: Found geeks starting at position : 11 **4 String indexOf(String str, int strt)**:此方法**从**下标**strt处开始查找字符str在string中的下标**【不存在则返回-1】 解释代码: // Java code to demonstrate the working // of String indexOf(String str, int strt) public class Index4 { public static void main(String args[]) { // Initialising string String Str = new String("Welcome to geeksforgeeks"); // Initialising search string String subst = new String("geeks"); // print the index of initial character // of Substring aftr 14th position // prints 19 System.out.print("Found geeks(after 14th index) starting at position : "); System.out.print(Str.indexOf(subst, 14)); } } 输出: Found geeks(after 14th index) starting at position : 19 **5 其他相关的应用** Finding out if a given character (maybe anything upper or lower case) is a vowel or consonant. Implementation is given below: 解释代码 class Vowels { // function to check if the passed // character is a vovel public static boolean vowel(char c) { return "aeiouAEIOU".indexOf(c)>=0; } // Driver program public static void main(String[] args) { boolean isVowel = vowel('a'); // Printing the output if(isVowel) System.out.println("Vowel"); else System.out.println("Consonant"); } } 输出: Vowel [https_www.geeksforgeeks.org_java-string-indexof]: https://www.geeksforgeeks.org/java-string-indexof/
相关 JS中string方法中常用方法之八:String.prototype.indexOf() String.prototype.indexOf()返回指定值在字符串对象中首次出现的位置。 从 fromIndex 位置开始查找,如果不存在,则返回 -1。 语法:s Myth丶恋晨/ 2022年09月26日 01:59/ 0 赞/ 204 阅读
相关 java String 查找字符串是否存在方法 --> IndexOf 用法 IndexOf 用法 indexOf(Srting s) 从头开始检索 indexOf(String s ,int startpoint) 从startpoint处开 电玩女神/ 2022年07月18日 19:30/ 0 赞/ 722 阅读
相关 C# String.IndexOf 方法 [C\ String.IndexOf 方法][C_ String.IndexOf] Posted on 2010-07-19 17:52 [ ykhi][ykhi] 阅读( 1 小咪咪/ 2022年07月16日 05:23/ 0 赞/ 171 阅读
相关 Java String indexOf()方法 Java String indexOf()方法 ¥ 我要打赏 [Java教程][Java] 作者:freeself 评论:0 条 Java技术QQ群:[22727 今天药忘吃喽~/ 2022年06月01日 00:47/ 0 赞/ 278 阅读
相关 java练习——int indexOf(String ,int) 救基友记2 Time Limit: 1000 ms Memory Limit: 65536 KiB [Submit][] [Statistic][] Problem 刺骨的言语ヽ痛彻心扉/ 2022年05月25日 22:43/ 0 赞/ 137 阅读
相关 解读ArrayList中的indexOf(Object o)方法 源码如下: / Returns the index of the first occurrence of the specified elemen 小鱼儿/ 2022年03月28日 15:12/ 0 赞/ 214 阅读
相关 String 中的 indexOf(String str) 方法解读 1、通过看源码,发现最终调用的是下面这个方法 static int indexOf(char[] source, int sourceOffset, int sour 亦凉/ 2022年01月10日 15:15/ 0 赞/ 232 阅读
相关 Java中String indexOf() 方法解读 翻译自[https://www.geeksforgeeks.org/java-string-indexof/][https_www.geeksforgeeks.org_java 朱雀/ 2021年12月07日 12:13/ 0 赞/ 571 阅读
相关 【Java源码解读】String中的equals方法 //简单来说,就是 String 重写了 Object 的 equals 方法,把引用比较改成了值比较。 public boolean equals(Objec 悠悠/ 2021年09月28日 23:58/ 0 赞/ 379 阅读
相关 Java中字符串indexof()的使用方法 有四种方法可以在Java(indexof())中找到字符串中的子字符串 indexOf方法返回一个整数值,该值指示子字符串在String对象中的开始位置。如果未找到子字... 朱雀/ 2021年05月23日 09:10/ 10 赞/ 10815 阅读
还没有评论,来说两句吧...