Prevent line break in interpolated string - dart

I having long string with interpolation in code. I am writing it as:
"""
$a: aaaaa
$b: bbbbb
$c: ccccc
"""
I do not want to write it in one line because readability. But in Text widget it displays also in column:
How to prevent line breaking?

If you use a multi-line string literal, then you will get newlines in the string.
You can remove them by doing:
var string = """
$a: aaaaa
$b: bbbbb
$c: ccccc
""".removeAll("\n");
That should leave you without any newlines.
Alternatively you can use multiple adjacent string literals instead:
var string =
"$a: aaaa"
"$b: bbbb"
"$c: cccc";
Adjacent string literals are combined into a single string value, and there are no extra newlines (if you want them, you have to add \n yourself). You also don't have to worry about leading whitespace on the lines.

Related

I'm trying to remove backslash from string but if I print out with print I get the correct string , but if I print it with "po" I get the same string

MyString = "CfegoAsZEM/sP\u{10}\u{10}}"
MyString.replacingOccurrences(of: "\"", with: "")
with print(MyString) I got this : "CfegoAsZEM/sP" (that's what I need)
with po MyString (on the debugger) : "CfegoAsZEM/sP\u{10}\u{10}}"
\u{10} is a linefeed character
Maybe a better way is to trim the string, it removes all whitespace and newline characters from the beginning and the end of the string
let myString = "CfegoAsZEM/sP\u{10}\u{10}"
let trimmedString = myString.trimmingCharacters(in: .whitespacesAndNewlines)
Your string doesn't contain literal backslash characters. Rather, the \u{} sequence is an escaped sequence that introduces a Unicode character. This is why you can't remove it using replacingOccurrences.
In this case, as Vadian pointed out it is the "new line" character (0x10). Since this is an invisible "white space" character you don't see it when you print the string, but you do see it when you use po. The debugger shows you escape sequences for non-printable characters. You will also see the sequence if you print(MyString.debugDescription)
Unfortunately the trimmingCharactersIn function doesn't appear to consider Unicode sequences.
We can use the filter function to examine each character in the string. If the character is ASCII and has a value greater than 31 ( 32 is the space character, the first "printable" character in the ASCII sequence) we can include it. We also need to ensure that values that aren't ASCII are included so as not to strip printable Unicode characters (e.g. emoji or non-Latin characters).
let MyString = "CfegoAsZEM/sP\u{10}\u{13}$}πŸ”…\u{1F600}".filter { $0.asciiValue ?? 32 > 31 }
print(MyString.debugDescription)
print(MyString)
Output
"CfegoAsZEM/sP}πŸ”…πŸ˜€"
CfegoAsZEM/sP}πŸ”…πŸ˜€
asciiValue returns an optional, which is nil if the character isn't plain ASCII. I have used a nil-coalescing operator to return 32 in this case so that the character isn't filtered.
I modified the initial string to include some printable Unicode to demonstrate that it isn't stripped by the filter.

How to convert string to raw string in dart

I want to convert an existing string to raw string.
like:
String s = "Hello \n World"
I want to convert this s variable to raw string(I want to print exact "Hello \n Wrold")
I need backslash(\) in output. I am trying to fetch string value from rest api. it have bunch of mathjax(latex) formula containing backslash.
Thanks
You are asking for a way to escape newlines (and possibly other control characters) in a string value.
There is no general way to do that for Dart strings in the platform libraries, but in most cases, using jsonEncode is an adequate substitute.
So, given your string containing a newline, you can convert it to a string containing \n (a backslash and an n) as var escapedString = jsonEncode(string);. The result is also wrapped in double-quotes because it really is a JSON string literal. If you don't want that, you can drop the first and last character: escapedString = escapedString.substring(1, escapedString.length - 1);.
Alternatively, if you only care about newlines, you can just replace them yourself:
var myString = string.replaceAll("\n", r"\n");

How to send an HTML email to the user when a button is pressed? (Swift) [duplicate]

Can someone please tell me how can I print something in following way "with" double quotes.
"Double Quotes"
With a backslash before the double quote you want to insert in the String:
let sentence = "They said \"It's okay\", didn't they?"
Now sentence is:
They said "It's okay", didn't they?
It's called "escaping" a character: you're using its literal value, it will not be interpreted.
With Swift 4 you can alternatively choose to use the """ delimiter for literal text where there's no need to escape:
let sentence = """
They said "It's okay", didn't they?
Yes, "okay" is what they said.
"""
This gives:
They said "It's okay", didn't they?
Yes, "okay" is what they said.
With Swift 5 you can use enhanced delimiters:
String literals can now be expressed using enhanced delimiters. A string literal with one or more number signs (#) before the opening quote treats backslashes and double-quote characters as literal unless they’re followed by the same number of number signs. Use enhanced delimiters to avoid cluttering string literals that contain many double-quote or backslash characters with extra escapes.
Your string now can be represented as:
let sentence = #"They said "It's okay", didn't they?"#
And if you want add variable to your string you should also add # after backslash:
let sentence = #"My "homepage" is \#(url)"#
For completeness, from Apple docs:
String literals can include the following special characters:
The escaped special characters \0 (null character), \ (backslash), \t
(horizontal tab), \n (line feed), \r (carriage return), \" (double
quote) and \' (single quote)
An arbitrary Unicode scalar, written as
\u{n}, where n is a 1–8 digit hexadecimal number with a value equal to
a valid Unicode code point
which means that apart from being able to escape the character with backslash, you can use the unicode value. Following two statements are equivalent:
let myString = "I love \"unnecessary\" quotation marks"
let myString = "I love \u{22}unnecessary\u{22} quotation marks"
myString would now contain:
I love "unnecessary" quotation marks
According to your needs, you may use one of the 4 following patterns in order to print a Swift String that contains double quotes in it.
1. Using escaped double quotation marks
String literals can include special characters such as \":
let string = "A string with \"double quotes\" in it."
print(string) //prints: A string with "double quotes" in it.
2. Using Unicode scalars
String literals can include Unicode scalar value written as \u{n}:
let string = "A string with \u{22}double quotes\u{22} in it."
print(string) //prints: A string with "double quotes" in it.
3. Using multiline string literals (requires Swift 4)
The The Swift Programming Language / Strings and Characters states:
Because multiline string literals use three double quotation marks instead of just one, you can include a double quotation mark (") inside of a multiline string literal without escaping it.
let string = """
A string with "double quotes" in it.
"""
print(string) //prints: A string with "double quotes" in it.
4. Using raw string literals (requires Swift 5)
The The Swift Programming Language / Strings and Characters states:
You can place a string literal within extended delimiters to include special characters in a string without invoking their effect. You place your string within quotation marks (") and surround that with number signs (#). For example, printing the string literal #"Line 1\nLine 2"# prints the line feed escape sequence (\n) rather than printing the string across two lines.
let string = #"A string with "double quotes" in it."#
print(string) //prints: A string with "double quotes" in it.

Java Scanner Delimiters

I am scanning a text file using Scanner and the next() method and adding each individual word to an ArrayList. How can I use delimiters to ignore punctuation? I have words such as:
cat,
dog.
"mouse
I want to remove the comma, period and quotation marks in these words respectively. How can I do this?
You can split a string on a regex using the String.split method, so you could pass a regex with all the punctuation you don't want to it, in the case you mentioned it would be
String[] words = "cat, dog. \" mouse".split("[\\\",\\.]");
Although in this case words would contain some empty values so to add it to your array you would need to do something like:
for (String word : words) {
if(!word.isEmpty()) {
arrayList.add(word);
}
}

Ruby Regex - Need to replace every occurrence of a character inside a regex match

Here's my string:
mystring = %Q{object1="this is, a testyay', asdkf'asfkd", object2="yo ho', ho"}
I am going to split mystring on commas, therefore I want to (temporarily) sub out the commas that lie in between the escaped quotes.
So, I need to match escaped quote + some characters + one or more commas + escaped quote and then gsub the commas in the matched string.
The regex for gsub I came up with is /(".*?),(.*?")/, and I used it like so:
newstring = mystring.gsub(/(".*?),(.*?")/ , "\\1|TEMPSUBSTITUTESTRING|\\2"), but this only replaces the first comma it finds between the escaped quotes.
How can I make it replace all the commas?
Thanks.
I believe this is one way to achieve the results you are wanting.
newstring = mystring.gsub(/".*?,.*?"/) {|s| s.gsub( ",", "|TEMPSUBSTITUTESTRING|" ) }
It passes the matched string (the quoted part) to the code block which then replaces all of the occurrences of the comma. The initial regex could probably be /".*?"/, but it would likely be less efficient since the code block would be invoked for each quoted string even if it did not have a comma.
Don't bother with all that, just split mystring on this regex:
,(?=(?:[^"]*"[^"]*")*[^"]*$)
The lookahead asserts that the comma is followed by an even number of quotes, meaning it's not inside a quoted value.

Resources