This question already has answers here:
Converting to upper and lower case in Java
(6 answers)
indexOf Case Sensitive?
(19 answers)
Closed 8 years ago.
String s ="Ganesh";
System.out.println(""+s.indexOf("ga"));
System.out.println(""+s.indexOf("Ga"));
When I run this, I get -1 for s.indexOf("ga") and 0 for s.indexOf("Ga")
I want to get 0 for both.
How to achieve this?
Unfortunately, there is no indexOfIgnoreCase() method, but you can achieve the same with using toLowerCase().
System.out.println(""+s.toLowerCase().indexOf("ga"));
You can convert the string you're looking for to lower or upper case when using indexOf, this way you'll always get the correct index regardless of casing
String s = "Ganesh";
String s2 = "ga";
String s3 = "Ga";
System.out.println(s.toLowerCase().indexOf(s2.toLowerCase()));
System.out.println(s.toLowerCase().indexOf(s3.toLowerCase()));
> 0
> 0
You can even put it into your own method like this:
public int indexOfIgnoreCase(String s, String find) {
return s.toLowerCase().indexOf(find.toLowerCase());
}
Then you can use it like this:
String s = "Ganesh";
System.out.println(indexOfIgnoreCase(s, "ga"));
System.out.println(indexOfIgnoreCase(s, "Ga"));
> 0
> 0
use the toLowerCase() or the toUpperCase() method of the String class, before you use the indexOf().
To use indexOf case-insensitive you can convert the Strings to lowercase first:
System.out.println("" + s.toLowerCase().indexOf("ga".toLowerCase()));
System.out.println("" + s.toLowerCase().indexOf("Ga".toLowerCase()));
Related
This question already has answers here:
How to get the substring that contains the first N unicode characters in Java
(2 answers)
Closed 5 years ago.
I have a String that could contain 4 bytes characters. For example:
String s = "\uD83D\uDC4D1234\uD83D\uDC4D";
I also have a size that I should use to get a substring from it. The size is in characters. So let's say that size is 5, so I should get the first 4 bytes character along with "1234".
Directly using substring as s.substring(0, 5) gives the wrong result returning the first character and just "123".
I could manage to get the right result using code points this way:
String s = "\uD83D\uDC4D1234\uD83D\uDC4D";
StringBuffer buf = new StringBuffer();
long size = 5;
s.codePoints().forEachOrdered(charInt -> {
if(buf.codePoints().count() < size) {
buf.appendCodePoint(charInt);
}
});
I bet there should be a way better and more efficient code to achieve this.
You can use offsetByCodePoints in order to help find the index of the character following 5 code points, and then use that as the second parameter to substring:
String s = "\uD83D\uDC4D1234\uD83D\uDC4D";
String sub = s.substring(0, s.offsetByCodePoints(0, 5));
Ideone Demo
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
I ave a code where I want to use != sign. But since I am using String, How do I not equals Sign. Hers is my code. So I want all this statement not to equal to each other so it can print Tie Game.
if (Array[0] == Array[currentPlayer] && Array [1] ==
Array[currentPlayer] && !Array [2] == Array[currentPlayer])
The above code is when everything equals to each other. But I want this statements not to equal each other.
Keep in mind that I have not used Int or Char, I am using String.
For string inequality, negate a call to the equals method using the !:
String x = "ABC";
String y = "XYZ";
if(!x.equals(y)) {
//do stuff
}
! can be used to negate ANY boolean expression, and String.equals returns a boolean.
You can do something like:
if (!Array[0].equals(Array[currentPlayer]) && !Array[1].equals(Array[currentPlayer])
&& Array[2].equals(Array[currentPlayer]))
Use equals() if you want case sensitive match meaning it will look at case of string as well when matching.
If you want case insensitive matching you can use equalsIgnoreCase() method in place of equals()
This question already has answers here:
Java String split removed empty values
(5 answers)
Closed 8 years ago.
I am spliting the String by tab like
String s = "1"+"\t"+2+"\t"+3+"\t"+"4";
System.out.println("length : "+ s.split("\\t").length);
In this case i get the length 4. But if i remove the last element 4 & give only blank, like
String s = "1"+"\t"+2+"\t"+3+"\t"+"";
System.out.println("Length : "+ s.split("\\t").length);
In this case i got the output 3. it means this is not calculating last tab.
In below case also, i need the length 4. This scenario i am using in my project & getting undesired result.
So please suggest me, How to calculate the entire length of tab delimited string, whether the last element is also blank.
Such as, if the case is,
String s = "1"+"\t"+2+"\t"+3+"\t"+"";
System.out.println("Length : "+ s.split("\\t").length);
then the answer should be 4 & if the case is,
String s = "1"+"\t"+2+"\t"+3+"\t"+"" + "\t"+ "";
System.out.println("Length : "+ s.split("\\t").length);
then the answer should be 5.
Please provide me the appropriate answer.
To prevent split from removing trailing empty spaces you need to use split(String regex, int limit) with negative limit value like
split("\t", -1)
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
I'm trying to write a simple hangman game in java for my college class. For some reason the if statement never returns seems to think that the two substrings being compared are equal. The two print statements show that by all rights the two should equate.
public String guessLetter(String letter)
{
String blanks = "";
String theWord="FOO";
for(int i=0; i<=theWord.length()-1; i++)
{
System.out.print(letter.substring(0,1).toUpperCase());
System.out.print(theWord.substring(i,i+1)+ "\n");
if((letter.substring(0,1).toUpperCase())==(theWord.substring(i,i+1)))
{
blanks = blanks + theWord.substring(i,i+1);
}
else
{
blanks = blanks + "___ ";
}
}
return blanks;
}
EDIT - As a great many people have pointed out, when comparing Strings, one must use the equals method instead of ==. I was unaware.
You are comparing a String so use "String".equals() dont use ==
use like this:
if((letter.substring(0,1).toUpperCase()).equals(theWord.substring(i,i+1)))
Java dont have == for string
you must use string1.equals(string2) function
if((letter.substring(0,1).toUpperCase())==(theWord.substring(i,i+1))) \ this is wrong for strings
When you compare strings you should use .equals or .equalsIgnorecase
if((letter.substring(0,1).toUpperCase()).equals(theWord.substring(i,i+1)))
ans also checkout the difference between == and .equals in java good explanation is given there.
This question already has answers here:
In Java, how do I check if a string contains a substring (ignoring case)? [duplicate]
(6 answers)
Closed 9 years ago.
I am writing a program to compare a few characters with a Char Array and return the index of the array. Is there any possible way to compare ignore case?
For example below:
String in = "I AM A HAPPY BOY";
char[] cha = new char[] {a,c,e,g,i,k,h,m,o,q,s,u,w,y};
char testChar = in.substring(4,5).charAt(0);
for(int a = 0; a<char.length; a++){
if(cha[a] == testChar)
return a+1;
}
I am unable to get the index as it will always point to 0. Is there anyway to ignore case here? Appreciate some advise.
Use Character.toLowerCase on both characters:
if (Character.toLowerCase(cha[a]) == Character.toLowerCase(testChar)) {
// logic here
}
As a side note, you could get away with the first toLowerCase if all the characters in your array are already lower case, or even use toLowerCase on the initial string and avoid both.
You can use Character.toLowerCase(char):
if (Character.toLowerCase(cha[a]) == Character.toLowerCase(testChar)) {
return a+1;
}
use Character.ToLowerCase(char c) before testing for equality.
in=in.toLowerCase();
However the most efficient way to convert chars between cases is to flip the 6th bit (ASCII values differ by 32).