String comparision
String a = "alpha";
String b = "xxxxalphayyyyyyy";
is there any method that returns true, if I had to check if String b contains a, otherwise false.....
What version of Java are you using?
edit: never mind, you got spoonfed in the meantime, I see
Edited by: georgemc on Jul 22, 2008 7:12 PM
Similar Messages
-
String Comparision in XSLT Mapping
HI All,
Can any one suggest me to compare the two strings in XSLT mapping.Please let me know is there any string comparision function in xslt?
Thanks
PullaraoHi Pullaro!
It depends what you want to do with the comparison
E.g. you can use a simple xsl-if Tag to compare a value
and then take action.
If your XML looks like this:
<root>
<child><b>value1</b></child>
<child>value2</child>
</root>
you can make an expression like this and take action on it:
<xsl:if test="<b>//child = 'value1'</b>">
<i>.. Do what you want to do here..</i>
</xsl:if>
With kind regards
Sebastian -
String comparision using tokens (challenging question)
Hello, everyone.
i am having a problem with string comparision.
for example, the input file is:
each line is an input line (string)
1)a x
2)b c x
3)a b x
4)a b y
5)a b c x
in above example a,b,c,x,y are strings(tokens)
in above example
line 1 ,3,5 has token "a" common and class "x" is also common
if this condition is satisfied,
line 1 should be send to general file (output file 1) and
line 3 and 5 should be send to specific file (output file 2)
rest of the lines should be send to third file.
remember in line 4, token "a" is same when compared with line1
but it has a different class (y) .this line will go the third file. this is where line 3 and 4 differs.
i have read this file into an string array.
how to proceed further.
i am new to java programming.
cheers in advance.Well, first off, I'm assuming that you keep the first string as the first token and the class name as your last token, then you tokenize everything using StringTokenizer. the code would look something like this. You could also use countTokens() to know how many tokens are in a tokenizer.
My approach would be the following:
1. Create an array of Strings to hold each line (you already did that)
2. Go through the array, one line at a time, tokenize the String, figure out the first and last tokens
3. Write it to the corresponding file.
The code would be something as follows
String ClassToLookFor, StringToLookFor; // holds which class and first string to look for
String firstToken, lastToken; //used in checking each element with first one
boolean firstFound = false; // First instance of Class name and first thread combo found
public static void main(String args[])
StringTokenizer tokenizer;
int tokenizerLength;
// read in array of Strings, store it into lines or something
int endOfRun = lines.length;
for (int i = 0; i<endOfRun; i++)
tokenizer = new StringTokenizer(lines);
tokenizerLength = tokenizer.countTokens();
if(!firstFound)
// store the information (StringToLookFor and ClassToLookFor) and write the line to the corresponding file
else
firstToken = tokenizer.next();
int runThru;
while(runThru < (tokenizerLength - 2)) {tokenizer.next();} // going to the last token
lastToken = tokenizer.next();
if (firstToken.equals(StringToLookFor) && lastToken.equals(ClassToLookFor))
{write lines[i] to second file}
else
{write lines[i] to third file}
hope that helps ... -
Using StartsWith for String Comparision in Business rules
hi,
I need to compare whether a particular string starts with some pre-defined prefixes. These are bound to change from time to time and hence we wanted to use the business rule engine for this. We declared prefixes using "list of values" vocab and
defined the set. Now, I hoped to use the String.StartsWith() method but couldn't understand if it can be used.
As of now, we are creating a method and that will be called with 2 parameters. One the string and other is one of the list of values which we want the string to start with. Is there any better way to do this?
Praveen Behara
MCST : BizTalk Server 2006 R2, 2010Hi Murugesan,
I need to match a particular series of numbers... say 12xxx,345xxx,567xxx. I am creating a
Series as a list and the valid values would be 12, 345, 567. I intend to keep them as prefixes i.e. they are not regular expressions. So, they won't be 12.*, 345.*, 567.*. The idea is to keep the vocab close to requirement
and away from implementation (typically, for a business user). Now, if I want this setup, how will the implementation with
Match look like?
Technically, your above solution would work.. but my way of implementation / thought process is for a different purpose.
Praveen Behara
MCST : BizTalk Server 2006 R2, 2010 -
Program for string comparision in ABAP
Hi,
I require a program in abap for string comparisionComparing Strings
Similarly to the special statements for processing strings, there are special comparisons that you can apply to strings with types C, D, N, and T. You can use the following operators:
<operator>
Meaning
CO
Contains Only
CN
Contains Not only
CA
Contains Any
NA
contains Not Any
CS
Contains String
NS
contains No String
CP
Contains Pattern
NP
contains No Pattern
There are no conversions with these comparisons. Instead, the system compares the characters of the string. The operators have the following functions:
CO (Contains Only)
The logical expression
<f1> CO <f2>
is true if <f1> contains only characters from <f2>. The comparison is case-sensitive. Trailing blanks are included. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of the first character of <f1> that does not occur in <f2> .
CN (Contains Not only)
The logical expression
<f1> CN <f2>
is true if <f1> does also contains characters other than those in <f2>. The comparison is case-sensitive. Trailing blanks are included. If the comparison is true, the system field SY-FDPOS contains the offset of the first character of <f1> that does not also occur in <f2>. If it is false, SY-FDPOS contains the length of <f1>.
CA (Contains Any)
The logical expression
<f1> CA <f2>
is true if <f1> contains at least one character from <f2>. The comparison is case-sensitive. If the comparison is true, the system field SY-FDPOS contains the offset of the first character of <f1> that also occurs in <f2> . If it is false, SY-FDPOS contains the length of <f1>.
NA (contains Not Any)
The logical expression
<f1> NA <f2>
is true if <f1> does not contain any character from <f2>. The comparison is case-sensitive. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of the first character of <f1> that occurs in <f2> .
CS (Contains String)
The logical expression
<f1> CS <f2>
is true if <f1> contains the string <f2>. Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the offset of <f2> in <f1> . If it is false, SY-FDPOS contains the length of <f1>.
NS (contains No String)
The logical expression
<f1> NS <f2>
is true if <f1> does not contain the string <f2>. Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of <f2> in <f1> .
CP (Contains Pattern)
The logical expression
<f1> CP <f2>
is true if <f1> contains the pattern <f2>. If <f2> is of type C, you can use the following wildcards in <f2>:
for any character string *
for any single character +
Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the offset of <f2> in <f1> . If it is false, SY-FDPOS contains the length of <f1>.
If you want to perform a comparison on a particular character in <f2>, place the escape character # in front of it. You can use the escape character # to specify
characters in upper and lower case
the wildcard character "" (enter # )
the wildcard character "" (enter # )
the escape symbol itself (enter ## )
blanks at the end of a string (enter #___ )
NP (contains No Pattern)
The logical expression
<f1> NP <f2>
is true if <f1> does not contain the pattern <f2>. In <f2>, you can use the same wildcards and escape character as for the operator CP.
Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of <f2> in <f1> .
DATA: F1(5) TYPE C VALUE <f1>,
F2(5) TYPE C VALUE <f2>.
IF F1 <operator> F2.
WRITE: / 'Comparison true, SY-FDPOS=', SY-FDPOS.
ELSE.
WRITE: / 'Comparison false, SY-FDPOS=', SY-FDPOS.
ENDIF.
The following table shows the results of executing this program, depending on which operators and values of F1 and F2.
<f1>
<operator>
<f2>
Result
SY-FDPOS
'BD '
CO
'ABCD '
true
5
'BD '
CO
'ABCDE'
false
2
'ABC12'
CN
'ABCD '
true
3
'ABABC'
CN
'ABCD '
false
5
'ABcde'
CA
'Bd '
true
1
'ABcde'
CA
'bD '
false
5
'ABAB '
NA
'AB '
false
0
'ababa'
NA
'AB '
true
5
'ABcde'
CS
'bC '
true
1
'ABcde'
CS
'ce '
false
5
'ABcde'
NS
'bC '
false
1
'ABcde'
NS
'ce '
true
5
'ABcde'
CP
'b'
true
1
'ABcde'
CP
'#b'
false
5
'ABcde'
NP
'b'
false
1
'ABcde'
NP
'#b'
true
5
goto sap library if intsalled in ur system and check the above one.... -
HI All,
I have a requirement to compare strings.
Like this:
parameters: belnr like bkpf-belnr.
Select * from swwwihead into table t_swwwihead
where ( WI_TYPE EQ 'F' or
WI_TYPE EQ 'W' ) AND
WI_CD GE l_budat.
X2 = belnr.
Loop at t_swwwihead.
X1 = t_swwwihead-TEXT.
IF X1 byte-cs X2.
write :/ t_swwwihead-TEXT.
Endif.
Endloop.
I tried to use 'CP' but i could not compare using fields. I mean i have to compare the values in the fields.
I tried byte-cs but that i can only compare in terms of bytes and my text doesnot have zeros at the begining. and xstring pads zeros to the string to make it a full byte it seems.
how can i compare this ?Hi,
Try to concatenate * in the beginning and in the end of the to be compared string before using CP..
Example
data: X1 type string.
X1 = 'Approve Parked document 100638133 initiated by USER123'.
data: X2 type string.
X2 = '100638133'.
CONCATENATE '' X2 '' INTO X2.
IF X1 CP X2.
write: / 'True'.
ELSE.
write: / 'False'.
ENDIF.
Thanks,
Naren
Message was edited by: Narendran Muthukumaran -
String Comparision in pl/sql
Hi,
i executed the below code and found that output should be 5 but it shows 7(always else part). it is not comparing string in case. can anybody state the reason.
declare
check_in_date date;
var_day varchar2(11);
rate_id number;
begin
check_in_date := '23-mar-2013';
select to_char(check_in_date, 'DAY') into var_day from dual;
dbms_output.put_line(var_day);
select case
when VAR_DAY = 'SATURDAY' THEN
5
WHEN VAR_DAY = 'SUNDAY' THEN
6
ELSE
7
END
into rate_id
from dual;
DBMS_OUTPUT.PUT_LINE(rate_id);
end;
Thanks.
VipinChange this
select to_char(check_in_date, 'DAY') into var_day from dual;as
select to_char(check_in_date, 'fmDAY') into var_day from dual;You need to use the "fm" (format model) specificaton otherwise the value will be right padded with space for the max size.
SQL> select '"' || to_char(sysdate+level, 'DAY') || '"' without_fm,
2 '"' || to_char(sysdate+level, 'fmDAY') || '"' with_fm
3 from dual
4 connect by level <= 7;
WITHOUT_FM WITH_FM
"FRIDAY " "FRIDAY"
"SATURDAY " "SATURDAY"
"SUNDAY " "SUNDAY"
"MONDAY " "MONDAY"
"TUESDAY " "TUESDAY"
"WEDNESDAY" "WEDNESDAY"
"THURSDAY " "THURSDAY"
7 rows selected. -
String comparision in reverse order
Hi all
i want a logic in plsql which compares string and substring
and soon from last digit in the number to first digit.
If condition mets gives out put
for example my input is
INPUT = 1234567890
TO COMPARE WITH a column has list of values like
1234560000
1234567000
1234567800....soon
so now the condition is
the input 1234567890 should match with the column value
1234567890
if value is not there it should match with
123456789
if not 12345678 soon till 1 in reverse comparisionIn SQL:
SQL> ed
Wrote file afiedt.buf
1 with t as (select '123456' col1 FROM dual)
2 SELECT col1,inp,lvl FROM
3 (SELECT SUBSTR(:my_num,1,level-1) inp,level - 1 lvl FROM
4 dual CONNECT BY level <= LENGTH(:my_num)) x,t
5* WHERE t.col1 = x.inp
SQL> /
COL1 INP LVL
123456 123456 6
SQL> ed
Wrote file afiedt.buf
1 with t as (select '1234567890' col1 FROM dual)
2 SELECT col1,inp,lvl FROM
3 (SELECT SUBSTR(:my_num,1,level-1) inp,level - 1 lvl FROM
4 dual CONNECT BY level <= LENGTH(:my_num) + 1) x,t
5* WHERE t.col1 = x.inp
SQL> /
COL1 INP LVL
1234567890 1234567890 10
-- in plsql
SQL> ed
Wrote file afiedt.buf
1 declare
2 my_val VARCHAR2(100) := NULL;
3 BEGIN
4 FOR I IN REVERSE 1..LENGTH(:my_num) LOOP
5 DBMS_OUTPUt.PUT_LINE('substring value:'||SUBSTR(:my_num,1,I));
6 BEGIN
7 SELECT '123456' INTO my_Val FROM dual WHERE '123456' = SUBSTR(:my_num,1,I);
8 EXCEPTION
9 WHEN NO_DATA_FOUND THEN
10 NULL;
11 END;
12 EXIT WHEN my_Val IS NOT NULL;
13 END LOOP;
14 DBMS_OUTPUT.PUT_LINE('matched value:'||my_val);
15* END;
SQL> /
substring value:1234567890
substring value:123456789
substring value:12345678
substring value:1234567
substring value:123456
matched value:123456
PL/SQL procedure successfully completed.
SQL> print :my_num
MY_NUM
1234567890 -
Good morning folks,
I want to take basically month and year of sysdate and compare with month and year of field in database and compare them.
But it is not working.
select case when
(concat(TO_CHAR(sysdate, 'MM'), to_char(sysdate,'YYYY')) >
concat(to_char(max(as_of_date),'MM'), to_char(max(as_of_date),'YYYY'))) then 'Not Archived'
else 'Archived' end from historical_advs_curr_eom
can someone suggest me any other better way of doing?
May be I shouldn't convert sysdate to string?
Thanks much folks.
regards
nathanuser12131044 wrote:
May be I shouldn't convert sysdate to string?Exactly. A string is not a date. Nor a number. So comparisons using strings do not behave like comparisons using dates and numbers.
Always try and use data in its native format - do not convert it into another data type as that data type will often behave differently and not provide the expected answer.
It is also not a good idea from a performance perspective. If you have an index or a number columns, the index value is numeric. If you now do a conversion of the column to a string and compare using that, it renders that index that contains numbers and not strings, useless.
Conversion also is an overhead - especially when it needs to be applied to every single row that the SELECT statement hints.
So rather stick to the native data type of that column. It is a lot more sensible. -
String Comparision ---please do help me
Hai all,
I have a problem ,
1. i have an ArrayList which contains a list of all properties in the app, and an HashMap which contains the name of the file as the key and the string representation of the file as the value. i need to search the key of the ArrayList in the value of the HashMap(String representation of the file)
I have written the code as follows and its not working to get the values
Code:
HashMap hashReport=new HashMap();
//Get the resource File Keys in the form of an ArrayList
ArrayList rList=getResourceFiles(); // name of a function
//Get the source Files and the String Representation of the
//Source File in the form of a HashMap.
HashMap sourcemap=createSourceMap(); // name of a function
Set sourceSet=sourcemap.keySet();
Iterator arrIter=rList.iterator();
while(arrIter.hasNext())
String arrString=(String)arrIter.next();
Iterator sourceIter=sourceSet.iterator();
while(sourceIter.hasNext())
String sourceStr=(String)sourceIter.next();
String valueStr=(String)sourcemap.get(sourceStr);
if ((arrString.indexOf(valueStr))!= -1)
if(hashReport.put(arrString,sourceStr)== null)
hashReport.put(arrString,sourceStr);
else
hashReport.put(arrString , ", "+ sourceStr);
please do help me in the regard.hashReport.put(arrString , ", "+ sourceStr);This does not append sourceStr to the existing value. It just puts sourceStr, with a comma in front of it, in hashReport, overwriting any value that was already associated with arrString. If you want to append, you'll need to save the value from when you checked it earlier:
if(hashReport.put(arrString,sourceStr)== null)By the way, that kinds of make the following statement kind of extraneous. You only need to do extra work if the result of the first put is not null.
The approach you're taking may not be ideal anyway. Rather than creating long comma-delimited Strings, another approach you could take is to use Lists as the values of the result map, like this:
if ((valueStr.indexOf(arrString))!= -1) {
List val = hashReport.get(arrString);
if (val == null) {
val = new ArrayList();
hashReport.put(arrString, val);
val.add(sourceStr);
} -
Basic Doubt regarding string comparision
I have the following code:
RegisterForm rf = (RegisterForm)form;
String user = rf.getUsername();
DTO ourdto = DBconnection.getDTO();
String pass = ourdto.getPword();When I print user,*pass* both the strings print "*pojo*"
Now the condition I have to satisfy is
pseudo code ---- if(*user* isequal to pass) { proceed forward }.
I have used .equals but it is returning false,used equalsIgnoreCase and isContentEquals both are returning false.
Could someone tell me if there is any method that can compare "pojo" to "pojo" and return a true.
.sidster wrote:
I have used .equals but it is returning false,used equalsIgnoreCase and isContentEquals both are returning false.Then they're not equal.
Could someone tell me if there is any method that can compare "pojo" to "pojo" and return a true.
String str = getUserInputOrWhatever();
if ("pojo".equals(str)) {
// the string was "pojo"
} -
Hi,
How could we compare two <u>character strings</u> having different size.
ex: name1(5)
name2(30).
name2 is a sap table field , but it stores data from user up to 5 characters only, i cann't modify name2 filed,
I am using
If (name1 = name2)
else.
endif.
Is it good practice.
Please let me know ur good suggestions.
Thanks.
Rao.report ychatest line-size 350.
data : name1(5) value 'CHAND',
name2(30) value 'CHANDRASEKHAR'.
if name1 eq name2+0(5).
write : 'OK'.
else.
write : 'NOT OK'.
endif. -
Strings comparision and get unique string in pure sql,
Dear all,
Here is two strings
S1='A,B,C,D,F';
s2='C,F,H,B,A,K';
output should be like unique string values ..
S3= A,B,C,D,F,H,K;
How to get this in pure sql..
thanks in advance,
RootsHi,
In a relational database, each column of each row should store one value, not a repleating group of values, such as a delimited list. This is so basic to database design that it is called "First Normal Form". You don't have to follow rules like this, but, if you don't, your code will be complicated, inefficient, and error-prone. It sould be best to re-design your application so that each value was on a separate row.
If you can't do that, then you can start by splitting your delimitd lists into separate rows. Then you can easily fond the distinct values, and use any String Aggregation technique to combine the results into one output row.
Here's one way to do all that:
WITH got_params AS
SELECT 'A,B,C,D,F' AS str, 1 AS str_id FROM dual UNION ALL
SELECT 'C,F,H,B,A,K', 2 FROM dual
, got_part_cnt AS
SELECT str
, 1 + LENGTH (str)
- LENGTH (REPLACE (str, ',')) AS part_cnt
FROM got_params
, cntr AS
SELECT LEVEL AS n
FROM (
SELECT MAX (part_cnt) AS max_part_cnt
FROM got_part_cnt
CONNECT BY LEVEL <= max_part_cnt
, got_substr AS
SELECT DISTINCT
REGEXP_SUBSTR ( p.str
, '[^,]+'
, 1
, c.n
) AS sub_str
FROM got_part_cnt p
JOIN cntr c ON c.n <= p.part_cnt
, got_r_num AS
SELECT sub_str
, ROW_NUMBER () OVER (ORDER BY sub_str) AS r_num
, ROWNUM AS r
FROM got_substr
SELECT MIN ( SUBSTR ( SYS_CONNECT_BY_PATH (sub_str, ',')
, 2
) AS unique_sub_strs
FROM got_r_num
WHERE CONNECT_BY_ISLEAF = 1
-- START WITH r_num = 1
CONNECT BY r_num = 1 + PRIOR r_num
;This works in Oracle 10.2.0.2.0 Express Edition, which is the only database I can use right now. For the main query, you should be able to say:
SELECT SUBSTR ( SYS_CONNECT_BY_PATH (sub_str, ',')
, 2
) AS unique_sub_strs
FROM got_r_num
WHERE CONNECT_BY_ISLEAF = 1
START WITH r_num = 1
CONNECT BY r_num = 1 + PRIOR r_num
;but, when I try that on my database, I only get 'A,B' as the output. CONNECT BY is very buggy in Oracle 10.2; if you have Oracle 10.1, the simpler form might work.
The query above can be shortened some, but I wrote it this way to make it easier to understand.
You can, for example, combine the sub-queries got_sub_str and got_r_num. If you do, use DENSE_RANK instead of ROW_NUMBER.
This does not assume that the sub-qtrings are all 1-character long. If they are, then the query can be simplified.
For more about string aggregation, see
http://www.oracle-base.com/articles/10g/StringAggregationTechniques.php -
Dropdownbyindex string comparision
Hi All,
I have a dropdown with 15 states are listed and those are coming from the backend while executing the RFC. I have 5 states hard coded and check if this retrived dropdown state is equals to hard coded state value with AP,MH,UP,MP,MO. if the retriedved value matches hard coded state value then i have to fire an event, if not then no event required.
I will get the currrnt state as:
String stateValue = wdContext.currentContextElement.getState();
stateValue.equals()............................?
this retrived state value needs to comapre with hard coded 5 states say exampel: AP,MH,UP,MP,MO. if any one matches i have to raise an event or else not.
Please advice
Peter.Hi,
Use the below mentioned code:
String strStates []={"AP","MH","UP","MP","MO"};
for (int i=0;i<strStates.length();i++){
if(stateValue.equalsIgnoreCase(strStates<i>)&&! stateValue.equalsgnoreCase(retrievedValue)))
// fire an event
Hope this helps you.If not please throw some more light on second requirement.
regards,
amit bagati -
Hi all,
Thanks in Advance.
I here i have my data in one internal table called g_t_comp. so here i need to attach a screen on which i need to add text boxes.
1) source string
2) destination string
what i need to do is from my data in the internal table. i need to replace the source string with the destination string into my internal table.
FOR EXAMPLE....
i have data in my ITAB like.
govardhan
rajesh
govarrao
rajan
rajasekhar
so
if i give source string as 1)gov
and destination as 2)hello
my ITAB shoul be filled with
helloardhan
rajesh
helloarrao
rajan
this is the situation......
please help me out......HI,
see this code.
data:begin of itab occurs 0,
field(30),
end of itab.
PARAMETERS:source(10),dest(10).
itab = 'GOVARDHAN'.
append itab.
itab = 'RAJESH'.
append itab.
itab = 'GOVARRAO'.
append itab.
itab = 'RAJAN'.
append itab.
itab = 'RAJASEKHAR'.
append itab.
loop at itab.
write:/ itab.
endloop.
loop at itab.
replace source in itab-field with dest IGNORING CASE.
modify itab.
endloop.
SKIP.
loop at itab.
write:/ itab.
endloop.
rgds,
bharat.
Maybe you are looking for
-
Error -5002 when adding linked user defined field using DI API
Hello, When I try to add a linked user defined field using DI API I get the error number -5002 with description: "The field 'Related Table' should consist of 8 alphanumeric characters with no valid or default values" I Get the error when I use the Ad
-
Moving checkbox items into other group
I have a very simple Word doc that when I have Acrobat automatically create a form from it, puts checkbox items into groups I need to change. For example, the grouping should be GroupA Item1 Item2 Item3 Item4 but what I get is GroupA Item1 Item2 Grou
-
Idoc To JDBC Szenario EOIO overtaking messages
Hi guys, I have an idoc to EOIO scenario, where some messages of different idoc types upsmas & chrmas have to be processed in the same order they are created in the SAP ERP Backend. To achieve this behaviour I use the the option "rule name" in the pa
-
Headset on 6085 stopped working
Hi. Couldn't really find any info specific to my problem, or at least, not via search. I got my phone a few weeks ago, and up until then, my headset worked fine. Today it cut out as I was talking to someone, and had to open up the phone to use it. No
-
AirportExtreme as wireless router through DSL modem on PC - not working.
Hello all. I've searched through several forums trying to find something could answer my question, but nothing came of it. Hopefully you can help, Our family has just recently purchased 2 new macbooks (love 'em!) and have been able to join our wirles