Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0

    check a string to see if it contains a specific word


    how do i check a string to see if it contains a specific word.

    Eg1: Shawn bin ahmad. i need to find the word "bin" and prompt user that bin exist.
    Eg2: Shawn binte ahmad. i need to find the word "binte" and prompt user that bin exist.
    Eg3: Shawn B. in this case, i need not check if "B" exist of not.
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Code:
    if ( inputString.indexOf( "bin" ) > -1 ) {
      out.print( "found it" );
    } else {
      out.print( "not found" );
    }
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0
    i was already using the method, however, i encountered problems with the bin and binte check.

    Codes
    ---------------------------------------------------------------------------
    boolean checkspecialword1 = fullname.indexOf("binte") > 0;
    if (checkspecialword1)
    {
    out.print("BINTE found in name.");
    }

    if(chinese.equalsIgnoreCase("Y"))
    {
    //check to see if there is "bin"
    //if yes, do the following
    if(checkbin == true)
    {
    out.println("Ok.");
    }
    //check to see if there is "bin"
    //if no, do the following
    else if(checkbin == false)
    {
    out.println("Ok.");
    }
    //if no, do the following
    else
    {
    out.println("Non chinese name should not contain 'bin'.");
    }
    }
    //if it is not a chinese name
    else
    { //check to see if there is "bin" //if yes, do the following
    if(checkbin == true)
    {
    out.println("Non chinese name should not contain 'bin'.");
    }
    //if no, do the following
    else
    {
    out.println("Ok.");
    }
    }
    ---------------------------------------------------------------------------------
    The problem is when i enter bin, the error msg "Binte found" is prompt and if i entered binte, the same error msg is prompt too. why is this so??

    i am trying to allow user to enter bin if it is a chinese name, however if it is a non-chinese name, bin is not allowed. And both chinese and non-chinese names, binte is not allowed.
  6. #4
  7. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Well, 'bin' is in 'binte', so it will be found both ways. You can try looking for ' bin '. That should be unique. Also, make sure you check that indexOf > -1, since it can return 0 as a valid result.
  8. #5
  9. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0
    but i need to check for both bin and binte.. is there a way whereby u can check the whole word?
  10. #6
  11. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Psuedo code:
    Code:
    if (containsBin) {
      if (containsBinte) {
         ..has binte...
      } else {
         ..has bin but not binte...
    }
  12. #7
  13. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0
    it still doesn't work.
    Can u help me check if there is anything wrong with this code..

    Codes:
    ---------------------------------------------------------------------------------

    //if it is a chinese name
    if(chinese.equalsIgnoreCase("Y"))
    {
    //is bin
    if(checkbin)
    {
    //if it is binte
    if(checkspecialword1)
    {
    out.print("BINTE found in name.");
    }
    // but not binte
    else
    {
    out.println("Ok.");
    }
    }
    //is not bin
    else
    {
    // and it is not binte
    if(!checkspecialword1)
    {
    out.print("Ok.");
    }
    //it is binte
    else
    {
    out.print("BINTE found in name.");
    }
    }
    }
    //if it is not a chinese name
    else
    {
    //if it is binte
    if(checkspecialword1)
    {
    out.print("BINTE found in name.");
    }
    //if it is bin
    else if(checkbin)
    {
    out.println("Non chinese name should not contain 'bin'.");
    }
    //if not bin or binte
    else
    {
    out.print("Ok.");
    }
    }

    -----------------------------------------------------------------------------------

    thanks.. get back to me asap...
  14. #8
  15. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0
    or maybe this??

    -----------------------------------------------------------------------------------
    if(chinese.equalsIgnoreCase("Y"))
    {
    //is bin
    if(checkbin)
    {
    out.println("Ok.");
    }
    //if it is binte
    else if(checkspecialword1)
    {
    out.print("BINTE found in name.");
    }
    //is not bin or binte
    else
    {
    out.println("Ok.");
    }
    }
    //if it is not a chinese name
    else
    {
    //if it is bin
    if(checkbin)
    {
    out.println("Non chinese name should not contain 'bin'.");
    }
    //if it is binte
    else if(checkspecialword1)
    {
    out.print("BINTE found in name.");
    }
    //if not bin or binte
    else
    {
    out.print("Ok.");
    }
    }
    ----------------------------------------------------------------------------------
  16. #9
  17. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Code:
    if(chinese.equalsIgnoreCase("Y"))
    {
    	//is bin
    	if(checkbin)
    	{
    		//if it is binte
    		if(checkspecialword1)
    		{
    			out.print("BINTE found in name.");
    		}
    		//not binte
    		else
    		{
    			out.println("Ok.");
    		}
    	//is not bin or binte
    	else
    	{
    		out.println("Ok.");
    	}
    }
    //if it is not a chinese name
    else
    {
    	//if it is bin
    	if(checkbin)
    	{
    		//if it is binte
    		if(checkspecialword1)
    		{
    			out.print("BINTE found in non chinese name.");
    		}
    		else
    		{
    			out.println("Non chinese name should not contain 'bin'.");
    		}
    	//if not bin or binte
    	else
    	{
    		out.print("Ok.");
    	}
    }
    Btw, use the code (#), or php (PHP) button to paste when pasting in code. Makes it easier to read.
  18. #10
  19. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0
    it still doesn't work.

    what i actually want is to check if the name that the user entered contains "BINTE" or "BIN". if the user entered a name with binte, no matter whether it is a chinese name or other name, i will reject it. however, if the user enter a name with bin, it will check whether it is a chinese name or non-chinese name using a name indicator.

    if it is a chinese name, i allow it, if i is not a chinese name, i reject it.
  20. #11
  21. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Code:
    if ( inputString.toLowerCase().indexOf( "bin" ) > -1 ) {
      if ( inputString.toLowerCase().indexOf( "binte" ) > -1 ) {
        out.print( "name not allowed" );
      } else {
        if ( isChinese ) {
          out.print( "welcome bin" );
        } else {
          out.print( "name not allowed" );
        }
      }
    }
    That work?
  22. #12
  23. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Here ya go:
    Code:
    <%@page contentType="text/html"%>
    <%!
    public String checkString( String inputString, boolean isChinese ) {
    	if ( inputString.toLowerCase().indexOf( "bin" ) > -1 ) {
    		if ( inputString.toLowerCase().indexOf( "binte" ) > -1 ) {
    			return( "binte" );
    		} else {
    			if ( isChinese ) {
    				return( "Chinese bin" );
    			} else {
    				return( "non-Chinese bin" );
    			}
    		}
    	} else {
    		return( "No bin or binte" );
    	}
    }
    %>
    <html>
    <head><title>Bin/Binte Check</title></head>
    <body>
    <p>Checking "Shawn bin ahmad" as Chinese<br>
    <b><%= checkString( "Shawn bin ahmad", true) %></b>
    </p>
    <p>Checking "Shawn bin ahmad" as not Chinese<br>
    <b><%= checkString( "Shawn bin ahmad", false) %></b>
    </p>
    <p>Checking "Shawn binte ahmad" as Chinese<br>
    <b><%= checkString( "Shawn binte ahmad", true) %></b>
    </p>
    <p>Checking "Shawn binte ahmad" as not Chinese<br>
    <b><%= checkString( "Shawn binte ahmad", false) %></b>
    </p>
    <p>Checking "Shawn ahmad" as Chinese<br>
    <b><%= checkString( "Shawn ahmad", true) %></b>
    </p>
    <p>Checking "Shawn ahmad" as not Chinese<br>
    <b><%= checkString( "Shawn ahmad", false) %></b>
    </p>
    </body>
    </html>
  24. #13
  25. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0
    both way does not work. it is because, .indexOf method checks for the first occurances of a string. and part of binte is bin thus, no matter if i enter bin or binte, the error prompt is still wrong. that is why i need a method to check for the exact length and word.
  26. #14
  27. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Ok, I think I'm missing what you're trying to do here. The code I posted last checks a string.
    If the string contains "binte" it prints "binte".
    If the string contains "bin" and is Chinese it prints "Chinese bin".
    If the string contains "bin" and is not Chinese, it prints "non-Chinese bin".
    And if the string doesn't contain "bin" or "binte" it prints "No bin or binte".

    If that's not what it's supposed to do, then what am I missing?
  28. #15
  29. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    19
    Rep Power
    0
    i don't really understand your codes. what i have is a interface whereby user can enter their names to subscribe for a service. therefore, when the user enters bin or binte in name, it checks (as previously mentioned.)

    the checks is done in another program and the fields from the interface are passed by posting the form page.

    is it possible that i send u the files then u help me take a look???
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo