This question already has answers here:
Does swift have a trim method on String?
(16 answers)
Closed 5 years ago.
In my app I have a text field, and when the user presses a button the text he have entered is added to a label. The problem is that if the user add an space at the end, the txt of the label looks bad.
Text field: "Diego "
Label: Great Diego !
Perform this on your String:
let trimmedString = yourstring.trimmingCharacters(in: .whitespacesAndNewlines)
It returns a new string made by removing from both ends of the String characters contained in a given character set, which in this case are whitesspaces and newlines. If you don't want to trim newlines, replace .whitespaceAndNewLines with .whitespace.
You can use String's trimming method passing in the characters to be trimmed. One way to do this is:
let string = " something "
string.trimmingCharacters(in: .whitespacesAndNewlines)
Which will remove whitespace and newlines from the beginning and the end of the string.
Related
How can I convert text with too many spaces to single space? For example name: "Brad Pit" there is only one space between first and last name but user can write "Brad (4 space) Pit" with too much space. How can I reduce this to single space in TextFormField?
Code:
void main() {
var name = 'Pratik Butani';
print(name);
print(name.replaceAll(new RegExp(r"\s+"), " "));
}
Output:
Pratik Butani
Pratik Butani
Explanation: Here I have used RegExp which will take continue spaces using \s+ and It will be replaced by one space.
You can this demo on DartPad
Do let me know if you have any confusion.
To reduce multiple space to a single space
Please refer to this answer
https://stackoverflow.com/a/1981366/9414608
Also you can prevent users from adding spaces in the TextFormField by using inputFormatters
Please refer to this answer
https://stackoverflow.com/a/59065304/9414608
flutterdartwhitespacestextformfieldstring
In Dart u can use:
print(yourString.trim())
to convert text with too many spaces
and u can try this
String name = 'Brad Pit ';
print(name.replaceAll(new RegExp(r"\s+"), ""));
I was parsing a messy XML. I found many of the nodes contain invisible characters only, for instance:
"\n "
" "
"\t "
"\n "
"\n\n"
I saw some posts and answers about alphabet and numbers, but the XML being parsed in my project includes UTF8 characters. I am not sure how I can list all visible UTF8 characters in the filter.
How can I determine if a string is made up of completely invisible characters like above, so I can filter them out? Thanks!
Use CharacterSet for that.
let nonWhitespace = CharacterSet.whitespacesAndNewlines.inverted
let containsNonWhitespace = (string.rangeOfCharacter(from: nonWhitespace) != nil)
Trim the string of whitespaces and newlines and see what's left.
if someString.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
// someString only contains whitespaces and newlines
}
I have a user enter a multi string in an NSTextView.
var textViewString = textView.textStorage?.string
Printing the string ( print(textViewString) ), I get a multi-line string, for example:
hello this is line 1
and this is line 2
I want a swift string representation that includes the new line characters. For example, I want print(textStringFlat) to print:
hello this is line 1\n\nand this is line 2
What do I need to do to textViewString to expose the special characters?
If you just want to replace the newlines with the literal characters \ and n then use:
let escapedText = someText.replacingOccurrences(of: "\n", with: "\\n")
This question already has answers here:
Remove last character from string. Swift language
(23 answers)
Remove Last Two Characters in a String
(5 answers)
Closed 5 years ago.
How do I remove the last character of a string in Swift 4? I used to use substring in earlier versions of Swift, but the substring method is deprecated.
Here's the code I have.
temp = temp.substring(to: temp.index(before: temp.endIndex))
dropLast() is your safest bet, because it handles nils and empty strings without crashing (answer by OverD), but if you want to return the character you removed, use removeLast():
var str = "String"
let removedCharacter = str.removeLast() //str becomes "Strin"
//and removedCharacter will be "g"
A different function, removeLast(_:) changes the count of the characters that should be removed:
var str = "String"
str.removeLast(3) //Str
The difference between the two is that removeLast() returns the character that was removed, while removeLast(_:) does not have a return value:
var str = "String"
print(str.removeLast()) //prints out "g"
You can use dropLast()
You can find more information on Apple documentation
A literal Swift 4 conversion of your code is
temp = String(temp[..<temp.index(before: temp.endIndex)])
foo.substring(from: index) becomes foo[index...]
foo.substring(to: index) becomes foo[..<index]
and in particular cases a new String must be created from the Substring result.
but the solution in the4kmen's answer is much better.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Regular Expression to match outer brackets
Can I use Perl regular expressions to match balanced text?
i would like to extract a whole text which has matching numbers of closing brackets. Here is the example text.
Big String 1 {sub string 1 {{something} something {}} sub string 2{something} sub string {{something}{something else}}}
Is there an easy way for me to extract whole string which belongs to Big String 1 only? The results i would like to have is as such:
Big String 1 = "sub string 1 {{something} something {}} sub string 2{something} sub string {{something}{something else}}"
Thanks,