String Not Equal to Something Else - string

I am working on android eclipse. This seems an easy question but I haven't found an answer of it yet! I have a string and I want to include in my if statement if that is not equal with something else. But only I know is the mystring.equals("example"). How do I enter if that string is not equal to something else? Thanks a lot

How do I enter if that string is not equal to something else?
Try this guy !. Example
if (!"someString".equals(myString)){
This tells it that if the value of myString isn't someString then it will enter the if condition. Doing it this way instead of
if (!myString.equals("someString"){
will protect against NPE so if myString is null you won't get an exception in the first example.

To be more precise, the String.equals() function is a function of return type boolean.
This means that it either returns true or false based on the values of the compared strings.
So:
if(myString.equals("other string"))
is equivalent to:
if(myString.equals("other string") == true)
To test for the contrary, you want:
if(myString.equals("other string") == false)
or the equivalent, as codeMagic mentionned: if(!myString.equals("other string"))

Related

Coding Bat Python Warmup

I'm new to python and just starting the journey to learn how to code. I answered a problem on coding bat, however my code was different than the solution. I would appreciate if someone could tell me why my code is not as good as the solution. Thanks!
Question: Given a string, return a new string where "not " has been added to the front. However, if the string already begins with "not", return the string unchanged.
My Code:
def not_string(str):
if 'not' in str[0:3]:
return str
else:
return ('not '+ str)
Answer:
def not_string(str):
if len(str) >= 3 and str[:3] == "not":
return str
return "not " + str
Your code is correct, however, there are a couple of differences between your code and the solution. To begin with, in the solution code, they check that the length of the string is greater than or equal to 3. Using the len(str) function, it will return the number of values in that string. For instance, if I had a string of value "codingbat", the length of str would be 9. It is good practice to check the length of the str before taking further action upon it. Furthermore, the solution checks from [:3], whereas your answer checks from [0:3]; these are the same thing but it is better practice to do [:3] as it shows more professionally that it is checking the first 3 values. Lastly, you had an else statement after your first if statement which was not necessary; as long as you make sure that whatever you wanted it to do if the if statement failed, is aligned with the first if command, then they will serve the same function but it is more professional to remove the "else" and rather back indent the else command saying that if the if statement failed it would do what you wanted it to do.
Keep in mind, that in any coding language, there are several ways to reach the desired solution. Other than that, great job!

Remove part of string (regular expressions)

I am a beginner in programming. I have a string for example "test:1" and "test:2". And I want to remove ":1" and ":2" (including :). How can I do it using regular expression?
Hi andrew it's pretty easy. Think of a string as if it is an array of chars (letters) cause it actually IS. If the part of the string you want to delete is allways at the end of the string and allways the same length it goes like this:
var exampleString = 'test:1';
exampleString.length -= 2;
Thats it you just deleted the last two values(letters) of the string(charArray)
If you cant be shure it's allways at the end or the amount of chars to delete you'd to use the version of szymon
There are at least a few ways to do it with Groovy. If you want to stick to regular expression, you can apply expression ^([^:]+) (which means all characters from the beginning of the string until reaching :) to a StringGroovyMethods.find(regexp) method, e.g.
def str = "test:1".find(/^([^:]+)/)
assert str == 'test'
Alternatively you can use good old String.split(String delimiter) method:
def str = "test:1".split(':')[0]
assert str == 'test'

How to compare strings in groovy script

I cannot understand why my simple String equality test is returning false.
Code is:
boolean isDevelopment() {
//config.project_stage is set to "Development"
String cfgvar = "${config.project_stage}"
String comp = "Development"
assert cfgvar.equals(comp)
}
Result is:
assert cfgvar.equals(comp)
| | |
| false Development
Development
I also get false if I do:
assert cfgvar == comp
toString() is not necessary. Most probably you have some trailing
spaces in config.project_stage, so they are retained also in cfgvar.
comp has no extra spaces, what can be seen from your code.
Initially the expression "${config.project_stage}" is of GString
type, but since you assign it to a variable typed as String,
it is coerced just to String, so toString() will not change anything.
It is up to you whether you use equals(...) or ==.
Actually Groovy silently translates the second form to the first.
So, to sum up, you can write assert cfgvar.trim() == comp.
You can also trim cfgvar at the very beginning, writing:
cfgvar = "${config.project_stage}".trim()
and then not to worry about any trailing spaces.
Have you checked for trailing spaces? At least your output as one for the first Development. Try a .trim() when you compare those strings (and maybe a .toLowerCase() too)
And remember: .equals() in Groovy is a pointer comparison. What want to do is ==. Yes, just the opposite from what it is defined in Java, but the Groovy definition makes more sense :-)
Update: see comment by #tim_yates - I mixed .equals() up with .is()
On of the objects you comparing is not a String but GString, try:
cfgvar.toString().equals(comp)
However your code works with groovy v. 2.4.5. Which version are you using?

Int32.TryParse equivalent for String?

I have been doing some homework. The task was to implement System.Int32.TryParse to check if an entered value is a number or something random. I was wondering if there is a built-in method that checks if something entered is a letter and NOT a number. I tried searching google and MSDN in the string type, but with no luck so far. I did write my own implementation, but I was curious.
Tnx
The easiest way to check whether a character is a number is probably to check whether it is in range from '0' to '9'. This works because of the way characters are encoded - the digits are encoded as a sub-range of the char values.
let str = "123"
let firstIsNumber =
str.[0] >= '0' && str.[0] <= '9'
This gives you a bit different behavior than Char.IsDigit because Char.IsDigit also returns true for thigns that are digits in other alphabets, say ႐႑႒႓႔႕႖႗႘႙ I suspect you do not plan to parse those :-).

intval($var) == strval($var) returns true

the given example is really simple so I don't think it needs any explaining.
I couldn't find any references on the docs that can explain this behaviour and I've also found a couple workarrounds for this, so you don't really need to bother finding them (thanks in advance though).
I'd just really like if some1 could explain this..... doesn't make any sense to me:
// comma separated IDs to later use in SQL statement
$var = '10,20,30,40,743,102394';
$multi_intval = intval($var); // same with (int) $var
$multi_string = strval($var); // same with (string) $var
var_dump($multi_intval, $multi_string, $multi_intval == $multi_string);
// result
int(10) string(22) "10,20,30,40,743,102394" bool(true)
how is 10 equal to a 22 strlen string?
I just ran across this looking for another answer, so even though it is old Ill give an answer in case someone else comes across it.
From the php docs here: If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number
Because of this your comparison intval($var) == strval($var) is changed to something like intval($var) == intval(strval($var)) which is of course equal (I don't know what the language is using to change the string to an integer, the above is just visual representation). If you really need to know if they are identical, use ===.

Resources