Comparing strings first letter

how to compare characters in a string in java
string compare java
compare first character of string java
c check first character of string
java compare strings alphabetically
how to compare characters in a string in c
string order comparison
how to compare two strings in java without using equals

how can i do this in java

if left(mystring,3)="adm" then ..

i've tried this but not success

if (mystring.charAt(3).equals("adm")) { ...

Could anyone tell me the correct way?

The method charAt() returns a primitive value char, the one at the given index. You can't call a method in a char value.

You could use

myString.startsWith("adm")

Instead.

The more important point is that all these methods are extensively documented. So don't assume what a method is doing. Study its javadoc instead.

Comparing strings first letter, The method charAt() returns a primitive value char, the one at the given index. You can't call a method in a char value. You could use myString. You don't. strcmp will compare the whole entire strings no matter what. Bonus hint: the first letter of name1 is name1[0] and the first letter of name2 is name2[0]. it does now compile. Im using pointers to a string (i have to).

You can use startsWith(string) like this :

if(mystring.startsWith("adm")){...

how do i compare first letters in two strings?, char *name1; char *name2;. user imputs the names. How do i use strcmp function to compare the first letters from two strings, so the function gives  Compares substrings of two specified String objects using the specified comparison options and culture-specific information to influence the comparison, and returns an integer that indicates the relationship of the two substrings to each other in the sort order.

You can achieve that using substring method

if (mystring.substring(0,3).equals("adm")) {

The String function substring(int j,int i) gives a substring of the original string from index j until index i, excluding character at i

String Length, String Comparison, Comparison of different strings up to n-th character - strncmp. If you want to compare first n characters of two strings, then strncmp can be used. Its return value is  Considers the string beginning at the index offset, and returns true if it begins with the substring specified as an argument. int compareTo(String anotherString) Compares two strings lexicographically.

Comparing Strings and Portions of Strings (The Java™ Tutorials , Returns true if and only if the argument is a String object that represents the same sequence of characters as this object, ignoring differences in case. boolean  None of the samples have used ReferenceEquals. This method determines if two strings are the same object. This can lead to inconsistent results in string comparisons. The following example demonstrates the string interning feature of C#. When a program declares two or more identical string variables, the compiler stores them all in the same

comparing to char from two diferent strings. (Beginning Java forum , im trying to compare the first characters of two strings. You are calling charAt(0) on firstLetter, which is a primitive char. What you try to do is  First, you get the string length of two cells using the LEN function, and then compare the numbers. Supposing the strings to be compared are in cells A2 and B2, use either of the following formulas: =LEN(A2)=LEN(B2)

Comparisons, If the first character from the first string is greater (or less) than the other Otherwise, if both strings' first characters are the same, compare the  Using the == (equal to) operator for comparing two strings If you simply require comparing the values of two variables then you may use the ‘==’ operator. If strings are same, it evaluates as True, otherwise False.

Comments
  • You probably want to write if(mystring.startsWith("adm"))
  • if (mystring != null && mystring.startsWith("adm")
  • See the API documentation of class java.lang.String.
  • Your first attempt, you cannot use = to perform a comparison. Also, you didn't define left. Your second attempt you are using 'charAt' which returns a single char which is a primitive and lacks methods. You could replace it with mystring.substring(0,3)