Compare individual char of 2 string
Hi
I need to compare the each individual char of 2 string.
String a = "Ae12";
String b = "aE12";
// I tried the followings:
// a
String c = a.substring(0,0);
String d = a.substring(1,1);
String e = a.substring(2,2);
String f = a.substring(3,3);
// b
String c1 = b.substring(0,0);
String d1 = b.substring(1,1);
String e1 = b.substring(2,2);
String f1 = b.substring(3,3);
if(c.equalsIgnoreCase(c1)&&d.equalsIgnoreCase(d1)&&e.equalsIgnoreCase(e1)&&f.equalsIgnoreCase(f1)) // Cannot be compared
// and I tried the followings:
// a
char c = a.charAt(0);
char d = a.charAt(1);
char e = a.charAt(2);
char f = a.charAt(3);
// b
char c1 = b.charAt(0);
char d1 = b.charAt(1);
char e1 = b.charAt(2);
char f1 = b.charAt(3);
if(c==c1&&d==d1&&e==e1&&f==f1) // cannot be compared.
Both cases cannot be compared. How can I fix it?
Because I want to compare those 2 strings without case sensitive and numbers.
Thanks
techissue2008 wrote:
String a = "Ae12";
String b = "aE12";
Then, are "Ae12" and "aE12" the same when ignore case?
If so, the Strings are actually the same.No, the Strings are not the same, if by same, you mean the "same" Object, or the "same" value when case is not ignored, but they are the "same" value when case is ignored.
This prints true (try it yourself), as it should
public class Test {
public static void main (String[] args) {
System.out.println("aE12".equalsIgnoreCase("Ae12"));
}They are still two distinct instances with two distinct values however, so where, exactly, is your problem?
Similar Messages
-
Comparing a char to a string value
hello.
i would like to take a string, take the first value, see if it is equal to something, and based on that condition, proceed through some conditional statements.
i tried this, but doesnt work.
String path;
char c = path.charAt( 0 );
if ( c.equals("/") ) {
Blah
ive also tried to cast the char as a String and it didnt work.
if ( ( (String)c ).equals("/") ) {i originally tried
any ideas?
thanks.
J>
any ideas?I always liked...
switch(path.charAt(0))
case '/': .... -
Comparing char in a string to strings in a txt file
Hi guys, i am dveloping a scrabble application for a project in jvava. Currently, i am trying to work on coding a computer player to play against a human, ive finished all the game logic nd 2 human players can successfully play on the same system.
My problem is when coming up with the AI logic, i am faced with the problem of how to compare the computer player's letters to the dictionary txt file in order to find the possible words he can play.
For example, the PC player has the letters A,B,E,G,T,F in his rack, it searches the dictionary and finds words that also contain these characters, so it would return words like AT, BAT, GATE, etc. I have already looke up the Jumble algorithm, but i dont really understand it(im still quite new to java).
I already have a ditionary class which i put the code below.
What i want to do now is just to come up with a simple method that takes a string, and compares the chars, and find words that match the char and return them but i dont knw hw to come about it.
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
import java.io.*;
public class Dictionary
ArrayList<String> dictionary;
public Dictionary() throws IOException
dictionary = new ArrayList<String>();
public void readInDictionaryWords() throws IOException
File dictionaryFile = new File("c:/data/dict.txt"); // declare the file
if( ! dictionaryFile.exists()) {
System.out.println("*** Error *** \n" +
"Your dictionary file has the wrong name or is " +
"in the wrong directory. \n" +
"Aborting program...\n\n");
System.exit( -1); // Terminate the program
Scanner inputFile = new Scanner( dictionaryFile);
while( inputFile.hasNext()) {
dictionary.add( inputFile.nextLine().toUpperCase() );
public boolean wordExists( String wordToLookup)
if( dictionary.contains( wordToLookup)) {
return true; // words was found in dictionary
else {
return false; // word was not found in dictionary
return"";
}Loops...
You take the first
then second
then third
and etc
You take the first 2
the next 2
the next 2
etc
all the way through n characters
once you have gone through this, then you have to do all possible combinations of characters of 2 through n.
Here is an implementation of N choose k for k up to 7:
public class Junk {
public Junk(){
public void choose1(String s){
int lCount=0;
for(int i=0; i<s.length(); i++){
System.out.println("("+s.charAt(i)+")");
lCount++;
System.out.println("Distinct Combinations: "+lCount);
public void choose2(String s){
int lCount=0;
for(int i=0; i<s.length(); i++){
for(int j=i+1; j<s.length(); j++){
System.out.println("("+s.charAt(i)+", "+s.charAt(j)+")");
lCount++;
System.out.println("Distinct Combinations: "+lCount);
public void choose3(String s){
int lCount=0;
for(int i=0; i<s.length(); i++){
for(int j=i+1; j<s.length(); j++){
for(int k=j+1; k<s.length(); k++){
System.out.println("("+s.charAt(i)+", "+s.charAt(j)+", "+s.charAt(k)+")");
lCount++;
System.out.println("Distinct Combinations: "+lCount);
public void choose4(String s){
int lCount=0;
for(int i=0; i<s.length(); i++){
for(int j=i+1; j<s.length(); j++){
for(int k=j+1; k<s.length(); k++){
for(int l=k+1; l<s.length(); l++){
System.out.println("("+s.charAt(i)+", "+s.charAt(j)+", "+s.charAt(k)+", "+s.charAt(l)+")");
lCount++;
System.out.println("Distinct Combinations: "+lCount);
public void choose5(String s){
int lCount=0;
for(int i=0; i<s.length(); i++){
for(int j=i+1; j<s.length(); j++){
for(int k=j+1; k<s.length(); k++){
for(int l=k+1; l<s.length(); l++){
for(int m=l+1; m<s.length(); m++){
System.out.println("("+s.charAt(i)+", "+s.charAt(j)+", "+s.charAt(k)+", "+s.charAt(l)+", "+s.charAt(m)+")");
lCount++;
System.out.println("Distinct Combinations: "+lCount);
public void choose6(String s){
int lCount=0;
for(int i=0; i<s.length(); i++){
for(int j=i+1; j<s.length(); j++){
for(int k=j+1; k<s.length(); k++){
for(int l=k+1; l<s.length(); l++){
for(int m=l+1; m<s.length(); m++){
for(int n=m+1;n<s.length(); n++){
System.out.println("("+s.charAt(i)+", "+s.charAt(j)+", "+s.charAt(k)+", "+s.charAt(l)+", "+s.charAt(m)+", "+s.charAt(n)+")");
lCount++;
System.out.println("Distinct Combinations: "+lCount);
public void choose7(String s){
int lCount=0;
for(int i=0; i<s.length(); i++){
for(int j=i+1; j<s.length(); j++){
for(int k=j+1; k<s.length(); k++){
for(int l=k+1; l<s.length(); l++){
for(int m=l+1; m<s.length(); m++){
for(int n=m+1; n<s.length(); n++){
for(int o=n+1; o<s.length(); o++){
System.out.println("("+s.charAt(i)+", "+s.charAt(j)+", "+s.charAt(k)+", "+s.charAt(l)+", "+s.charAt(m)+", "+s.charAt(n)+", "+s.charAt(o)+")");
lCount++;
System.out.println("Distinct Combinations: "+lCount);
public static void main(String[] args) {
Junk j = new Junk();
j.choose7("1234567");
}I'll leave it to you to integerate the algos into your app appropriately. -
How to take last 5 char from a string
Hi All
I have a requirement,
base on some comp i have to populate another field.
in this case i have compare last 3 char of a string
example if total string say asasdffrx i have to take last three char here it is frx.
always i have to last 3 char.
i can use +6(3) but it wont work every time because string lenght is not fix for all vaue.
thanksHi,
A.H.P sample is ok but insert additional check for strigs shorter than 3 (to avoid negative offset):
data : n TYPE I.
n = STRLEN( zfield ) - 3.
if n >= 0.
result = zfield+n(3).
else.
result = zfield.
endif.
Krzys -
Can you help me compare two chars?
I keep getting a 'cannt be dereferenced' error whenever I try compareing two chars. What exactly does this mean? Here's the code I'm useing:
import java.io.*;
public class Replace
public static void main(String[] args)
char let = 'h';
char let2 = 'i';
int temp = let.compareTo(let2);
}Thank you slappy for trying to make me leaern. In
future, try to give a little background to wwhat your
explaining. As you can see, I was useing character
methods on a char, and your reply just plain confused
me!Sir,
I know it confused you. But I did say HINT. And I did say char and Character and I was hoping that would twig. For your future reference in Java pimitives are lower case while Class Names Begin With A Capital Letter.
Well good luck.
Sincerely,
Slappy -
How to delete the last char in a String?
i want to delete the last char in a String, but i don't want to convert the String to a StringBuffer or an array, who knows how to do?
Try it in this way
String MyString = "ABCDEF";
MyString = MyString.substring(0,MyString.length()-1); -
Deleting the last char of a string
How can I Delete the last char of a string???
example:
asd@fdg@vvdfgfd@gdgdfgfd@gdfgdf@
I want delete the last @!
Tks in advance!hi,
try this:
lv_count = strlen(string).
lv_count_last = lv-count - 1.
replace string+lv_count_last(lv_count) in string by ' '.
This should work.
thnx,
ags.
Edited by: Agasti Kale on Jun 4, 2008 9:03 PM -
How to remove last char in a string by space
I have to implement backspace application(remove a last char in a string , when we pressed a button).
for ex: I enter the no
1234
instead of 1236
So when i press a button on the JWindow... it should display
123
so that i can enter 6 now.
I tried to display the string "123 " instead over "1234" but it is not working when the no background is specified. but works fine when a background color is specified.
The string is displayed as
Graphics2D g = (Graphics2D)window.getGraphics();
AttributedString as = new AttributedString(string, map);
g.drawString(as.getIterator(),x,y);
In the map, the background is set to NO_BACKGROUND.
Thanks and regardsDeja vu. I saw this kind of post before, and I'm sure
it was today.http://forum.java.sun.com/thread.jspa?threadID=588110&tstart=0
Here it is. -
Deleting last char in a string...
I have a string that is created as follows: strBuffer += k.getKeyChar();(k is KeyPressed ...KeyEvent) How can I delete the last Char in the string strBuffer?
Hi
// simply substring
if (strBuffer.length() > 1)
strBuffer = strBuffer.substring(0, strBuffer.length()-1);
// you may also use
if (strBuffer.length() > 1)
StringBuffer buffer = new StringBuffer(strBuffer).
deleteCharAt(strBuffer.length()-1);
strBuffer = buffer.toString();
}I've not tested the above code myself & I've written the above code on the go after reading your query. The above code definitely work & help you.
Regards,
JPassion -
Finding last char in the string
Hi
i need to find out whether the last char of a string is '/' (forward slash). please let me know if there's a FM to do that. Other post me the logic for this ASAP.
thankslen = strlen(char).
len = len - 1.
if char+len(1) = '/'.
*write ur code
endif.
chk this code
REPORT ychatest.
DATA : str(5) VALUE '123/',
len TYPE i.
len = STRLEN( str ).
len = len - 1.
IF str+len(1) = '/'.
WRITE : '/ found'.
ENDIF.
<b>Reward points if helpful and close thrad if solved</b>
Message was edited by: Chandrasekhar Jagarlamudi -
CS5.1 Menu - editing individual char attributes
I just realized that I cannot edit attributes of individual characters in menu text box.
If I highlight a portion of text in a box, then as soon as I click on Character panel the highlight disappears and entire box is selected (handles appear).
I can accomplish the task with "edit Menu in Photoshop", but I used to be able to do this within Encore CS3.
As a test, I installed Encore CS5.1 on 32bit XP partition containing CS3, and it behaved the same way (unable to edit attributes of individual chars).
I have not been able to find any info on this on the net. Am I missing something?
Thank you for any insight on this.
Production Premium CS5.5
on Win7 SP1 64bitThank you for the follow-up.
I select the text box with the Direct Selection Tool, ...... Now select the letter/word(s) you want to edit.
Yes, I get that far, and next you do is to click on one of the attributes on Character panel to change the appearance of selected text. As soon as I do that entire box gets selected, and attribute you select, say bold, is applied to the entire text, not the selected. This happens even if I hold my breath tight.
I zapped the Encore preference files, again with the same result.
I really don't remember if this was the case from the beginning (I installed CS5.5 in November last year) or it started doing this sometime after installation... -
Comparing Collection object with a string
Hi everyone,
There is my problem: I have a program that generate all possible combinations of a certain number of letters for example: -ABC- will gives A, B, C, AB, AC, BC, ABC. Everything is fine at this point. But then the user can input a string like "AB" and I have to tell him if this string is contained in the generated set. My set is in a Collection, but when I used : LetterCombination.Contains(TextString), it returns always false. Even if I know that the string is contained in the set.
Here is the code to generate the set:
public class Recursion
Collection LetterCombination;
/*String Letters is the letters which I have to generate the combination, and
the LetterNumber is the number of letters contained in my string.*/
public Recursion(Set ItemLetters, String Letters, int LetterNumbers)
ItemLetters = new TreeSet();
String[] Token = Letters.split(" ");
/*Adding the letters in the TreeSet*/
for (int i = 0; i < LetterNumbers; i++)
ItemLetters.add(Token);
LetterCombination = BuildLetterSet(ItemLetters);
private Collection BuildLetterSet(Set ItemLetters)
Set NotUsedYet = new TreeSet();
Set thisPowerSet = new TreeSet();
Collection Letterresult = new ArrayList();
NotUsedYet.addAll(ItemLetters);
BuildByRecursion(NotUsedYet, thisPowerSet, Letterresult);
return Letterresult;
private void BuildByRecursion(Set notUsedYet, Set thisPowerSet, Collection result)
if(notUsedYet.isEmpty())
if(!thisPowerSet.isEmpty())
Set copy = new TreeSet();
copy.addAll(thisPowerSet);
result.add(copy);
return;
Object item = notUsedYet.iterator().next();
notUsedYet.remove(item);
BuildByRecursion(notUsedYet, thisPowerSet, result);
thisPowerSet.add(item);
BuildByRecursion(notUsedYet, thisPowerSet, result);
thisPowerSet.remove(item);
notUsedYet.add(item);
And if I print out the LetterCombination collection, it gives me:
[C]
[B, C]
[A]
[A, C]
[A, B]
[A, B, C]
Which are the good combination needed. But I really don't understand how to compare this collection with the string entered by the user.
I'm really lost. can somebody help me! Thanks in advance...You don't show where you call this constructor, or what your arguments are:
public Recursion(Set ItemLetters, String Letters, int LetterNumbers)
ItemLetters = new TreeSet();
String[] Token = Letters.split(" ");
/*Adding the letters in the TreeSet*/
for (int i = 0; i < LetterNumbers; i++)
ItemLetters.add(Token[ i ]);
LetterCombination = BuildLetterSet(ItemLetters);
}But, the constructor doesn't make sense, anyway. itemLetters is immediately set to a new TreeSet, so whatever you passed in (if anything) for the first argument is unchanged by the constructor (and the new TreeSet is lost when you finish the constructor). Also, you split your input Letters on space, but rely on LetterNumbers for the length of the split array (which may or may not be accurate). Why not do a 'for' loop testing for "i < Token.length", and eliminate LetterNumbers parameter? Your input for the second parameter to this constructor would have to be "A B C D" (some letters delimited by spaces). Your constructor only needs one parameter--that String.
The capitalization of your variables doesn't follow standard coding conventions. Your code is hard to read without the "code" formatting tags (see "Formatting Tips"/ [ code ] button above message posting box). -
Hi,
In C there is a function strchr() to find a char in a string. Is there an equivalent funtion in Java?
The function indexOf() returns an int, but I want this char search would return a boolean value.
So help please, thanks a lot
chanhpublic boolean strchr(char myChar, String myString){
for(int j = 0; j < myString.length(); j++){
if(myChar == myString.charAt(j) return true;
return false;
}Is this what you are looking for? -
How to get a char from a String Class?
How to get a char from a String Class?
Use charAt(int index), like this for example:
String s = "Java";
char c = s.charAt(2);
System.out.println(c); -
How to invoke the char " in a string?
Hi!
I�m trying to invoke the char " in a string and I can�t think of any good way to do that.
plz help.use
String foo = new String("the string foo has the \"a string\" value");
Maybe you are looking for
-
Can anyone explain why the generated URL of my BPS web app
Errors as follows:- Business Server Page (BSP) error What happened? Calling the BSP page was terminated due to an error. SAP Note The following error text was processed in the system: Die URL enthält keine vollständige Domainangabe (cud015 statt cud0
-
In SAP Business By Design , in a customer account we can see two balances: 1. Open item Totals in Transaction Currency 2.Valuated Balance in Company Currency Why in some cases the two balances differ ?? Further for credit limit of Customer the balanc
-
Grid Control 11.1 install problem libXtst.so.6: cannot open shared object
I have a new server installing Grid Control 11.1. Linux RHEL 5 rel 2 64 bit. Installed database 11.2.0.1 on this server (applied patch set 9352237). Installed JDK 1.6 (following note 1063587.1) and WebLogic 10.3 (note 1063112.1). When launching the G
-
Lens profile in Camera raw won't load
Hello : Can you please tell me why, when in Camera raw, my lens profile won't load. I use photoshop CS6 and have a Nikon d700 lens is 70-200 2.8 both included in the profile available. All softwar is up to date Thank you !
-
Hello How to implement Gtalk and team viewer in OS Linux of ES4 Pls. provide your suggestion.. Thanks/regs Sunil