|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Code:
if ( inputString.indexOf( "bin" ) > -1 ) {
out.print( "found it" );
} else {
out.print( "not found" );
}
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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.
|
|
#5
|
|||
|
|||
|
but i need to check for both bin and binte.. is there a way whereby u can check the whole word?
|
|
#6
|
|||
|
|||
|
Psuedo code:
Code:
if (containsBin) {
if (containsBinte) {
..has binte...
} else {
..has bin but not binte...
}
|
|
#7
|
|||
|
|||
|
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... |
|
#8
|
|||
|
|||
|
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."); } } ---------------------------------------------------------------------------------- |
|
#9
|
|||
|
|||
|
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. |
|
#10
|
|||
|
|||
|
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. |
|
#11
|
|||
|
|||
|
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? |
|
#12
|
|||
|
|||
|
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>
|
|
#13
|
|||
|
|||
|
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.
|
|
#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? ![]() |
|
#15
|
|||
|
|||
|
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??? |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > check a string to see if it contains a specific word |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|