I have a string and i want to know if the last character in my string is #
example:
String test="test my String #";
I have a string and i want to know if the last character in my string is #
example:
String test="test my String #";
if(test.endsWith("#"))
Or, if you really want to do it manually (not a good idea)...
if(test.charAt(test.length()-1) == '#')
The following snippet should be instructive:
String[] tests = {
"asdf#",
"#asdf",
"sdf#f",
"#",
"",
"asdf",
};
String fmt = "%6s%12s%12s%12s%n";
System.out.format(fmt, "String", "startsWith", "endsWith", "contains");
for (String test : tests) {
System.out.format(fmt, test,
test.startsWith("#"),
test.endsWith("#"),
test.contains("#")
);
}
This prints:
String startsWith endsWith contains
asdf# false true true
#asdf true false true
sdf#f false false true
# true true true
false false false
asdf false false false
boolean startsWith(String prefix)
boolean contains(CharSequence s)
true
if and only if this string contains the specified sequence of char values.