Swift .Uppercasestring or .Lowercasestring Property Replacement

Swift .uppercaseString or .lowercaseString property replacement

Xcode 6.0 / Swift 1.0

String is bridged seamlessly to NSString, so it does have uppercaseString and lowercaseString properties as long as you import Foundation (or really almost any framework since they'll usually import Foundation internally. From the Strings and Characters section of the Swift Programming Guide:

Swift’s String type is bridged seamlessly to Foundation’s NSString
class. If you are working with the Foundation framework in Cocoa or
Cocoa Touch, the entire NSString API is available to call on any
String value you create, in addition to the String features described
in this chapter. You can also use a String value with any API that
requires an NSString instance.


Xcode 6.1 / Swift 1.1

As @newacct pointed out, in Xcode 6.1 / Swift 1.1, uppercaseString and lowercaseString are in Swift's String class so you don't need to use the ones defined in NSString. However, it's implemented as an extension to the String class in the Foundation framework so the solution is still the same: import Foundation

In a playground:

import Foundation

var sillyString = "This is a string!" // --> This is a string!
let yellyString = sillyString.uppercaseString // --> THIS IS A STRING!
let silentString = sillyString.lowercaseString // --> this is a string!

Swift 3.0

In a playground:

import Foundation

var sillyString = "This is a string!" // --> This is a string!
let yellyString = sillyString.uppercased() // --> THIS IS A STRING!
let silentString = sillyString.lowercased() // --> this is a string!

Swift apply .uppercaseString to only the first letter of a string

Including mutating and non mutating versions that are consistent with API guidelines.

Swift 3:

extension String {
func capitalizingFirstLetter() -> String {
let first = String(characters.prefix(1)).capitalized
let other = String(characters.dropFirst())
return first + other
}

mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}

Swift 4:

extension String {
func capitalizingFirstLetter() -> String {
return prefix(1).uppercased() + self.lowercased().dropFirst()
}

mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}

Replace part of string with lower case letters - Swift

Thanks to everyone for their help. In the end I couldn't get any of the solutions to work and after a lot of testing, I came up with this solution:

func correctStringWithUsernames(inputString: String, completion: (correctString: String) -> Void) {

// Create the final string and get all
// the seperate strings from the data.
var finalString: String!
var commentSegments: NSArray!
commentSegments = inputString.componentsSeparatedByString(" ")

if (commentSegments.count > 0) {

for (var loop = 0; loop < commentSegments.count; loop++) {

// Check the username to ensure that there
// are no capital letters in the string.
let currentString = commentSegments[loop] as! String
let capitalLetterRegEx = ".*[A-Z]+.*"
let textData = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
let capitalResult = textData.evaluateWithObject(currentString)

// Check if the current loop string
// is a @user mention string or not.

if (currentString.containsString("@")) {

// If we are in the first loop then set the
// string otherwise concatenate the string.

if (loop == 0) {

if (capitalResult == true) {

// The username contains capital letters
// so change it to a lower case version.
finalString = currentString.lowercaseString
}

else {

// The username does not contain capital letters.
finalString = currentString
}
}

else {

if (capitalResult == true) {

// The username contains capital letters
// so change it to a lower case version.
finalString = "\(finalString) \(currentString.lowercaseString)"
}

else {

// The username does not contain capital letters.
finalString = "\(finalString) \(currentString)"
}
}
}

else {

// The current string is NOT a @user mention
// so simply set or concatenate the finalString.

if (loop == 0) {
finalString = currentString
}

else {
finalString = "\(finalString) \(currentString)"
}
}
}
}

else {

// No issues pass back the string.
finalString = inputString
}

// Pass back the correct username string.
completion(correctString: finalString)
}

Its certainly not the most elegant or efficient solution around but it does work. If there are any ways of improving it, please leave a comment.

Replace part of string with lower case letters - Swift

Thanks to everyone for their help. In the end I couldn't get any of the solutions to work and after a lot of testing, I came up with this solution:

func correctStringWithUsernames(inputString: String, completion: (correctString: String) -> Void) {

// Create the final string and get all
// the seperate strings from the data.
var finalString: String!
var commentSegments: NSArray!
commentSegments = inputString.componentsSeparatedByString(" ")

if (commentSegments.count > 0) {

for (var loop = 0; loop < commentSegments.count; loop++) {

// Check the username to ensure that there
// are no capital letters in the string.
let currentString = commentSegments[loop] as! String
let capitalLetterRegEx = ".*[A-Z]+.*"
let textData = NSPredicate(format:"SELF MATCHES %@", capitalLetterRegEx)
let capitalResult = textData.evaluateWithObject(currentString)

// Check if the current loop string
// is a @user mention string or not.

if (currentString.containsString("@")) {

// If we are in the first loop then set the
// string otherwise concatenate the string.

if (loop == 0) {

if (capitalResult == true) {

// The username contains capital letters
// so change it to a lower case version.
finalString = currentString.lowercaseString
}

else {

// The username does not contain capital letters.
finalString = currentString
}
}

else {

if (capitalResult == true) {

// The username contains capital letters
// so change it to a lower case version.
finalString = "\(finalString) \(currentString.lowercaseString)"
}

else {

// The username does not contain capital letters.
finalString = "\(finalString) \(currentString)"
}
}
}

else {

// The current string is NOT a @user mention
// so simply set or concatenate the finalString.

if (loop == 0) {
finalString = currentString
}

else {
finalString = "\(finalString) \(currentString)"
}
}
}
}

else {

// No issues pass back the string.
finalString = inputString
}

// Pass back the correct username string.
completion(correctString: finalString)
}

Its certainly not the most elegant or efficient solution around but it does work. If there are any ways of improving it, please leave a comment.

Returning text as either Uppercase or Lowercase

It makes no sense to put the function into the enum and according to Exercise 5a it's not required.

enum UpLow {
case uppercase
case lowercase
}

func setCase (text: String, `case`: UpLow) -> String {
switch `case` {
case .uppercase: return text.uppercased()
case .lowercase: return text.lowercased()
}
}

let uppercaseString = setCase(text: "Interstellar", case: .uppercase)
let lowercaseString = setCase(text: "Interstellar", case: .lowercase)

How can I identify uppercase and lowercase characters in a string with swift?

In Swift 5, we can now check for character properties per Unicode standard.

For your question, chr.isUppercase and chr.isLowercase is the answer.

How can I set http in searchBar case insensitive?

Referencing this:

Here is a link to another stack overflow talking about uppercase and lowercase strings.

You should be able to put the whole string to lowercase by doing:

let address = searBar.text!.lowercaseString

That should convert the address string to all lowercase.
You might also want to consider changing the capitalization for the search bar box to be disabled.

Another solution might be to check if address.contains("Http://")

Best,
Joe

How to randomize the case of letters in a string in Swift?

Swift 2

I would use arc4random_uniform and map.

let myString = "Have a nice day!"

let result = myString.characters.map { (char) -> String in
if arc4random_uniform(2) == 0 {
return String(char).lowercaseString
}
return String(char).uppercaseString
}.joinWithSeparator("")

print(result)

hAvE A NICe DAy!

Swift 3:

let result = myString.characters.map {
if arc4random_uniform(2) == 0 {
return String($0).lowercased()
}
return String($0).uppercased()
}.joined(separator: "")

Swift 4.2

let result = myString.map {
if Int.random(in: 0...1) == 0 {
return String($0).lowercased()
}
return String($0).uppercased()
}.joined(separator: "")

tmpnam warning saying it is dangerous

From tmpnam manpage :

The tmpnam() function generates a different string each time it is called, up to TMP_MAX times. If it is called more than TMP_MAX times, the behavior is implementation defined.

Although tmpnam() generates names that are difficult to guess, it is nevertheless possible that between the time that tmpnam() returns a pathname, and the time that the program opens it, another program might create that pathname using open(2), or create it as a symbolic link. This can lead to security holes. To avoid such possibilities, use the open(2) O_EXCL flag to open the pathname. Or better yet, use mkstemp(3) or tmpfile(3).

Mktemp really create the file, so you are assured it works, whereas tmpnam returns a name, possibly already existing.



Related Topics



Leave a reply



Submit