How to search word by word in android

后端 未结 5 1985
我在风中等你
我在风中等你 2021-01-22 08:28

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 08:52

    For most simple usage, you can use a StringTokenizer Look at this link. http://docs.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html

    For using Regular expressions, Look at Patterns in android. http://developer.android.com/reference/java/util/regex/Pattern.html

    0 讨论(0)
  • 2021-01-22 08:55
    List<String> tokens = new ArrayList<String>();
    
    String text = "Samsung Galaxy S Two";
    StringTokenizer st = new StringTokenizer(text);
    
        //("---- Split by space ------");
        while (st.hasMoreElements()) {
            tokens.add(st.nextElement().toString());
        }
    
        String search = "axy";
        for(int i=0;i<tokens.size();i++)
        {
            if(tokens.get(i).contains(search))
            {
                System.out.println("Word is "+tokens.get(i));
                break;//=====> Remove Break if you want to continue searching all the words which contains `axy`
            }
        }
    
    output====>Galaxy
    
    0 讨论(0)
  • 2021-01-22 09:12

    I know this is an old question, but I am writing here so that the next person who needs help can be helped.

    You can use matches.

    String str = "Hello, this is a trial text";
    str1 = str.toLowerCase();
    if(str1.matches(".*trial.*")) //this will search for the word "trial" in str1
    {
        //Your Code
    }
    
    0 讨论(0)
  • 2021-01-22 09:17

    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
    
    0 讨论(0)
  • 2021-01-22 09:18

    use indexOf:

    int i= string.indexOf('1'); 
    

    or substring:

    String s=string.substring("koko",0,1);
    
    0 讨论(0)
提交回复
热议问题