Use String isEmpty to check for empty string - string

Swift Programming Language mentions using isEmpty to check for empty string. Are there cases where checking string against "" not yield the same result as using isEmpty?
In other words:
if str.isEmpty {
XCTAssert(str == "", "This should be true as well")
}
From the documentation:
Find out whether a String value is empty by checking its Boolean isEmpty property:
if emptyString.isEmpty {
print("Nothing to see here")
}

The empty string is the only empty string, so there should be no cases where string.isEmpty() does not return the same value as string == "". They may do so in different amounts of time and memory, of course. Whether they use different amounts of time and memory is an implementation detail not described, but isEmpty is the preferred way to check in Swift (as documented).

Related

Will it help in this problem to convert the string into a list?

I have a question. I am not looking for the answer to this exercise, just a pointer. My question is: will this be easier to solve if the two-word string is converted to a List?
ANIMAL CRACKERS: Write a function takes a two-word string and returns True if both words begin with same letter
animal_crackers('Levelheaded Llama') --> True
animal_crackers('Crazy Kangaroo') --> False
Yes, in this problem it would help to use the .split() method to split the string into a list of the two words.
Generally, the same data can be represented in different ways, and you want to use a representation which aligns with what you need to do with the data. Since this problem is about words, then "a list of words" aligns closer with what you need to do than "a string with two words" does.
Having a list of words allows you to write code that refers to "the first word" (i.e. words[0]) and "the second word" (i.e. words[1]). There is no comparably-simple way to refer to individual words in the original string.
Thanks everyone. I realised quite soon after i posted that the .split() function was what i needed to use. This was my solution.
def animal_crackers(string):
twoWords = (string.split(' '))
if twoWords[0][0].upper() == twoWords[1][0].upper():
return True
Do a split on the string input myListOfTwoSriting = stringInput.split(" "), next split the 2 string link that : firstletter = myListOfTwoSriting [0].split("")[0] then firstletterOfSeconde = myListOfTwoSriting [1].split("")[0] for the seconde.
next:
if firstletter == firstletterOfSeconde :
return True
else:
return False

Special case in the Unicode String.StartsWith function

I have implemented a utf8 string class in C++ with full case-folding case insensitive comparison. By full case-folding I mean a case insensitive comparison where Maße and MASSE would be equal. I use the same comparison function in the StartsWith(...) member function which returns true if it starts with the string specified in the argument. There is a special case where I'm not sure what is the correct behavior:
String myString = "Maß";
bool result = myString.StartsWith("MAS", CaseSensitivity::CaseInsensitive);
So, using full case folding I can convert Maß to mass and MAS to mas. So basically the StartsWith function should return true, because mass starts with mas. But the problem might be that not the whole ß was processed, only the half of it. Is this correct? Or should I return true only when the whole codepoint was processed?

How to check whether a string contains a substring in Kotlin?

Example:
String1 = "AbBaCca";
String2 = "bac";
I want to perform a check that String1 contains String2 or not.
Kotlin has stdlib package to perform certain extension function operation over the string, you can check this method it will check the substring in a string, you can ignore the case by passing true/false value. Refer this link
"AbBaCca".contains("bac", ignoreCase = true)
The most idiomatic way to check this is to use the in operator:
String2 in String1
This is equivalent to calling contains(), but shorter and more readable.
You can do it by using the "in" - operator, e.g.
val url : String = "http://www.google.de"
val check : Boolean = "http" in url
check has the value true then. :)
See the contains method in the documentation.
String1.contains(String2);
Kotlin has a few different contains function on Strings, see here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/contains.html.
If you want it to be true that string2 is contained in string1 (ie you want to ignore case), they even have a convenient boolean argument for you, so you won't need to convert to lowercase first.
For anyone out there like me who wanted to do this for a nullable String, that is, String?, here is my solution:
operator fun String?.contains(substring:String): Boolean {
return if (this is String) {
// Need to convert to CharSequence, otherwise keeps calling my
// contains in an endless loop.
val charSequence: CharSequence = this
charSequence.contains(substring)
} else {
false
}
}
// Uses Kotlin convention of converting 'in' to operator 'contains'
if (shortString in nullableLongString) {
// TODO: Your stuff goes here!
}

String Not Equal to Something Else

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"))

Checking and returning numeric field value?

To check whether value is numeric in LS, I would write the following code:
Dim strVal as String
Dim dVal as Double
strVal = doc.fldValue(0)
If IsNumeric(strVal) Then
dVal = cdbl(strVal)
End If
How would we achieve this within XPages? I just tried getItemValueString on a numeric field and it didn't work. getItemValueDouble worked, but as we all know we cannot rely on that field value to be numeric (in case some rogue agent is ran and converts a field on a particular document to be a text field by mistake!), so how do we check whether it's numeric or not in XPages? Is there an easy way to achieve this or do we have to use getItemValue and check through the vector object? Has anyone else noticed this?
Thanks.
In my experience when you are saving a numeric value it will be returned as a double value. If you want to be sure which datatype the .getitemvalue("field") value returns, you can use the .getClass() method on the returned value. for instance when you want to print it to the console:
var x = doc.getItemValue("fieldx").get(0)
if(x!=null){
print(x.getClass());
}
because of this you can use code like this:
var x = doc.getItemValue("fieldx").get(0) // get first value of the field
if(x != null){
if(x instanceof java.lang.Double){
}
if(x instanceof java.lang.Integer){}
}
or... you could just use
#IsNumber()
;)

Resources