Flutter remove extra white space between string - string

how to remove extra white space between following string ?
String word= "Hai where are you from" ;
word.split(" ") does not work for this condition.

this will remove the white space in the string
try
String word= "Hai where are you from";
print(word.replaceAll(' ', ''));

You can you two functions for String:
ReplaceAll Function
String str;
str.replaceAll(" ", " ");
Trim function
str.trim();

If you want to remove extra white spaces you can use this code. Just call the cleanupWhitespace method and it will return the cleaned up string.
final whitespaceRE = RegExp(r"(?! )\s+| \s+");
String cleanupWhitespace(String input) => input.split(whitespaceRE).join(" ");
It works like this for ex if you have a string like this:
Hello world
It will replace all the white space with a single white space.
Hello world

Related

Flutter - How to split string by space in the stateful widget?

I'm still new to coding and I don't know how to split string in the stateful widget.
to split String str = "Hello World!";
you can simply use split method like str.split(' ')
Refer this official documentation.
You can use String's .split() method to achieve this, passing in a string containing just a space as your delimiter. For example:
'Hello World'.split(' ') will return ['Hello', 'World'].

Kotlin - How to trim all leading spaces from a multiline string?

String.trim() does not work for strings built using buildString. For example,
val s = buildString {
append("{")
append('\n')
append(" ".repeat(5))
append("hello")
append(" ".repeat(7))
append("world")
append("}")
}
println(s.trim())
This prints
{
hello world}
but I need it to print
{
hello
world
}
How can I trim indent without writing my own trim method?
trim() only removes whitespaces from the beginning and end of a whole string, not per-line. You can remove spaces from each line with:
s.lineSequence()
.map { it.trim() }
.joinToString("\n")
Please note that as a side effect, above code converts all line endings to LF ("\n"). You can replace "\n" with "\r\n" or "\r" to get different results. To preserve line endings exactly as they were in the original string, we would need a more complicated solution.
One liner:
s.lines().joinToString(transform = String::trim, separator = "\n")
You could use a regular expression to trim leading whitespace:
val s = buildString {
append("{")
append('\n')
append(" ".repeat(5))
append("hello\n")
append(" ".repeat(7))
append("world\n")
append("}")
}
println(s.replace(Regex("""^\s+""", RegexOption.MULTILINE), ""))
Output:
{
hello
world
}

How to put double quotes into Swift String

I am writing some codes that deals with string with double quote in Swift. Here is what I've done so far:
func someMethod {
let string = "String with \"Double Quotes\""
dealWithString(string)
}
func dealWithString(input: String) {
// I placed a breakpoint here.
}
When I run the codes the breakpoint stopped there as usual but when I input the following into the debugger:
print input
This is what I get:
(String) $R0 = "String with \"Double Quotes\""
I got this string with the backslashes. But if I tried to remove the backslashes from the source, it will give me compile error. Is there a workaround for this?
You are doing everything right. Backslash is used as an escape character to insert double quotes into Swift string precisely in the way that you use it.
The issue is the debugger. Rather than printing the actual value of the string, it prints the value as a string literal, i.e. enclosed in double quotes, with all special characters properly escaped escaped.
If you use print(input) in your code, you would see the string that you expect, i.e. with escape characters expanded and no double quotes around them.
Newer versions of Swift support an alternate delimiter syntax that lets you embed special characters without escaping. Add one or more # symbols before and after the opening and closing quotes, like so:
#"String with "Double Quotes""#
Be careful, though, because other common escapes require those extra # symbols, too.
#"This is not a newline: \n"#
#"This is a newline: \#n"#
You can read more about this at Extended String Delimiters at swift.org.
extension CustomStringConvertible {
var inspect: String {
if self is String {
return "\"\(self)\""
} else {
return self.description
}
}
}
let word = "Swift"
let s = "This is \(word.inspect)"

Removing special characters from a string In a Groovy Script

I am looking to remove special characters from a string using groovy, i'm nearly there but it is removing the white spaces that are already in place which I want to keep. I only want to remove the special characters (and not leave a whitespace). I am running the below on a PostCode L&65$$ OBH
def removespecialpostcodce = PostCode.replaceAll("[^a-zA-Z0-9]+","")
log.info removespecialpostcodce
Currently it returns L65OBH but I am looking for it to return L65 OBH
Can anyone help?
Use below code :
PostCode.replaceAll("[^a-zA-Z0-9 ]+","")
instead of
PostCode.replaceAll("[^a-zA-Z0-9]+","")
To remove all special characters in a String you can use the invert regex character:
String str = "..\\.-._./-^+* ".replaceAll("[^A-Za-z0-1]","");
System.out.println("str: <"+str+">");
output:
str: <>
to keep the spaces in the text add a space in the character set
String str = "..\\.-._./-^+* ".replaceAll("[^A-Za-z0-1 ]","");
System.out.println("str: <"+str+">");
output:
str: < >

Blackberry Java replace char \n

I have an example string get from XML, contains: Hello\nWorld\n\nClick\nHere.\nThanks.
And then i want to replace the \n char with space char.
Already try using string replace, string substring, string indexOf. But cannot detect the \n char, iam trying using '\r\n' to detect, but didnt work.
String hello = "Hello\nWorld\n\nClick\nHere.\nThanks.";
String afterReplace = hello.replace('\n', ' ');
But still cannot remove/replace the \n with space.
Anyone can help me?
Thanks a lot.
If I understand correctly you have a string which when printed shows the \n characters and does not actually skip a line.
Hello\nWorld\n\nClick\nHere.\nThanks. would be represented in code by:
String s = "Hello\\nWorld\\n\\nClick\\nHere.\\nThanks."
Now s is equal to what you would obtain from your XML.
Try this:
String afterReplace = hello.replace('\\n', ' ');

Resources