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,
Related
This question already has answers here:
Check if string contains numeric value in PowerShell?
(7 answers)
Closed 1 year ago.
How can you check if the whole string is a number?
You can check if a string contains a number with regular expression. Can you use regular expressions as well to find out if a string is only a number, without any other characters?
"abc" is supposed to be false, it's not a number
"abc12" is supposed to be false, it's not a number
"123" is supposed to be true
if you are not using variables, and literal which is one quote, exactly what is in between the quotes.
'abc' -is [String]
'abc12' -is [String]
'123' -is [String]
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:
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.
This question already has answers here:
Create a string with n blank spaces or other repeated character
(4 answers)
Closed 6 years ago.
How can you create a String of a given length consisting of the same character - without using a loop?
Ie: create a String 10 characters in length where each character is an asterisk: **********
Similar to this approach in Java: new String(new char[n]).replace("\0", s);
There's a String initializer for that:
init(repeating repeatedValue: String, count: Int)
Description Creates
a new string representing the given string repeated the specified
number of times.
let string = String(repeating: "*", count: 10)
This question already has answers here:
How can I concatenate strings in a cell array with spaces between them in MATLAB?
(5 answers)
Closed 9 years ago.
I have a variable which is the list of string of a listBox handle
string = get(handles.ListBox,'string');
string=
'file1'
'file2'
I want to create a single string like:
line= 'file1 file2'
In order to write it in one line in my output file. The number of input files may vary in every case so I would like to do it in a general way. I have tried 'horzcat', 'strcat' but no success.
There is a strjoin function specifically for this task:
C = {'one', 'two', 'three'};
str = strjoin(C)
The simplest way, and IMHO cleanest way, is to make a "matrix" out of them:
s1 = 'bla';
s2 = 'blabla';
scat = [ s1, s2 ];
I found a nice way:
S = {'file1'; 'file2'};
strjoin(S(:)', ' ')