How to search word by word in android

后端 未结 5 1724
日久生厌
日久生厌 2021-01-22 08:20

How to search a word in a string?

For example

String text = \"Samsung Galaxy S Two\";

If I use text.contains(\"???\");<

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-22 09:12

    Try this..

    String string = "madam, i am Adam";
    

    // Characters

    // First occurrence of a c
    int index = string.indexOf('a');    // 1
    
    // Last occurrence
    index = string.lastIndexOf('a');    // 14
    
    // Not found
    index = string.lastIndexOf('z');    // -1
    

    // Substrings

    // First occurrence
    index = string.indexOf("dam");      // 2
    
    // Last occurrence
    index = string.lastIndexOf("dam");  // 13
    
    // Not found
    index = string.lastIndexOf("z");    // -1
    

提交回复
热议问题