This question already has an answer here:
How can I 0-pad a number by a variable amount when formatting with std::fmt?
(1 answer)
Closed 6 months ago.
To print a string centered by 15 dashes, i'd do the following:
println!("{:-^15}", "Hi :D");
But what if i wanted to control the number of dashes that are put before and after my string? I tried the following:
let margin: usize = 15;
println!("{:-^{margin}}", "Hi :(");
And other variations, but all of them don't compile. What should i do?
You almost had the syntax of formatting parameters. Here are a few ways to use them:
println!("{:-^1$}", "Hi :D", 15);
let margin = 15;
println!("{:-^1$}", "Hi :D", margin);
println!("{:-^margin$}", "Hi :D");
Related
This question already has answers here:
Why are empty strings returned in split() results?
(9 answers)
Closed 2 years ago.
I have this String and I want to separate it using the specified separator. Why is the length 4 instead of 3?
brain = " Know yourself! Understand yourself! Correct yourself! "
brain = brain.strip()
change = brain.split("yourself!")
print(len(change))
Because of your word that you want to split with that (yourself!) is at the end(or at the first) of your string.
you can try this solution :
def is_not_none(element):
if element is not None:
return element
brain = "know yourself! Understand yourself! Correct yourself! "
brain = brain.strip()
brain = list(filter(is_not_none, brain.rsplit('yourself!')))
print(brain)
This question already has answers here:
Why does my string not match when reading user input from stdin?
(3 answers)
Closed 3 years ago.
I am currently working on a simple "user input" program. The user can enter a number, which I get with
std::io::stdin().read_line(&mut let_name_here).ok().expect("Error");. After getting the user input I want to print it to the console for a review.
I have noticed strange behavior within the println! macro. The following code
println!("Your input: {}", let_name_here);
println!("Your input: {}", let_name_here);
outputs this:
Your input: 15
Your input: 15
Why is there an extra \n in the println! marco. From my coding experience I would assume the following:
Your input: 15
Your input: 15
But to achive this output I have to use folloing code:
print!("Your input: {}", let_name_here);
print!("Your input: {}", let_name_here);
I don't understand why the println! marco outputs\n twice. What would I do, if I want to \n at the end of the first line, with those two marcos it would not be possible. Am I missing something important?
It looks like there is a newline character at the end of the string you have read from stdin. The println macro is then adding a newline char to the end resulting in two newlines.
You should strip away the newline character from the end of the text read from stdin, or just use print if you don't want to do that.
This question already has an answer here:
Swift 3 incorrect string interpolation with implicitly unwrapped Optionals
(1 answer)
Closed 6 years ago.
Since Swift 3.0 I have some troubles with Strings, especially with concatenation. 1st example would be what I used since I started using Swift to define my url strings.
internal let host: String! = "https://host.io/"
let urlString = "\(host)oauth/access_token"
where host is defined as at the beginning of the class. This worked perfectly until Swift 3.0, now that prints out like this:
Optional("https://host.io/")oauth/access_token
which is very strange. Now I have to write this
let urlString = host + "oauth/access_token"
To get the expected output.
https://host.io/oauth/access_token
Another - I guess similar problem I'm having with Strings is this. I'm again concatenating strings but this time I'm using + ilke with urlString - but this time that doesn't work. Line of code looks like this:
self.labelName.text = currentUser.name + " " + String(describing: ageComponents.year)
which unfortunately produces string like this: "My Name Optional(26)". In this case I don't have a solution String(describing: ageComponents.year) is not an optional and it doesn't allow me to do things like String(describing: ageComponents.year) ?? "whatever"
Anyone seen something similar?
In Swift 3 all properties of the native struct DateComponents are optionals unlike the Foundation NSDateComponents counterparts.
var year: Int? { get set }
You need to unwrap it. If you specified the unit year in ageComponents you can do that safely.
This question already has answers here:
How do I decode HTML entities in Swift?
(23 answers)
Closed 7 years ago.
I have an RSS feed and in its description element (here: www.marketoloji.com/?feed=rss2) I have ascii characters for the ones like ' or & and they are seen as ’ / &. How can I render that description string in Swift so that I wont see ascii characters?
You can use NSAttributedString to easily convert html code for you using the NSHTMLTextDocumentType option:
extension String {
var htmlString:String {
return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil, error: nil)!.string
}
}
let htmlCode = "’ / &"
htmlCode.htmlString // "’ / &"
This question already has answers here:
Any way to replace characters on Swift String?
(23 answers)
Closed 8 years ago.
I want to replace some text in a string with another string.
For example : A string equals to "Red small car".
I want to replace "small" with "big" so the string becomes
"Red big car". I have to do this in swift. Thanks.
You can try using stringByReplacingOccurrencesOfString
let string = "Big red car"
let replaced = (string as NSString).stringByReplacingOccurrencesOfString("Big", withString: "Small")
Edit
In Swift 5
import Foundation
let string = "Big red car"
let replaced = string.replacingOccurrences(of: "Big", with: "Small")
You can use stringByReplacingOccurrencesOfString, e.g.
let s1 : String = "Red small car"
let s2 = s1.stringByReplacingOccurrencesOfString("small", withString: "big")