I have string as shown below. In dart trim() its removes the whitespace end of the string. My question is: How to replace spaces middle of string in Dart?
Example-1:
- Original: String _myText = "Netflix.com. Amsterdam";
- Expected Text: "Netflix.com. Amsterdam"
Example-2:
- Original: String _myText = "The dog has a long tail. ";
- Expected Text: "The dog has a long tail."
Using RegExp like
String result = _myText.replaceAll(RegExp(' +'), ' ');
In my case I had tabs, spaces and carriage returns mixed in (i thought it was just spaces to start)
You can use:
String result = _myText.replaceAll(RegExp('\\s+'), ' ');
If you want to replace all extra whitespace with just a single space.
To replace white space with single space we can iterate through the string and add the characters into new string variable by checking whitespace condition as below code.
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
void main() {
String str = "Dart remove empty space ";
String stringAfterRemovingWhiteSpace = '';
for (int i = 0; i < str.length; i++) {
if (!str[i].contains(' ')) {
stringAfterRemovingWhiteSpace = stringAfterRemovingWhiteSpace + "" + str[i];
}
}
print(stringAfterRemovingWhiteSpace);
}
Originally published at https://kodeazy.com/flutter-remove-whitespace-string/
Related
I have a string. In my code, I'm trying to trim substrings after a non-letter token if there are any. What do you think would be a better way to do that?
I tried split, replaceAll functions and matches function with regex but couldn't deliver a good solution.
String initialString = "Brown 1fox jum'ps over 9 the_t la8zy dog.";
String[] splitString = initialString.split(" ");
String finalString= new String();
for (int i = 0; i < splitString.length; i++) {
finalString+=splitString[i].split("[^a-zA-Z]",2)[0]+" ";
}
finalString=finalString.trim().replaceAll("\\s+", " ");
Actual Result (as expected): "Brown jum over the la dog"
As an alternative you might use [^a-zA-Z ]+\S*
to replace the matches with an empty string and after that replace the double whitespace characters with a single using \\s{2,}
String string = "Brown 1fox jum'ps over 9 the_t la8zy dog.";
String result = string.replaceAll("[^a-zA-Z ]+\\S*", "").replaceAll("\\s{2,}", " ");
Demo
All you have to do is this,
String initialString = "Brown 1fox jum'ps over 9 the_t la8zy dog.";
String resultStr = Stream.of(initialString.split(" "))
.map(s -> s.replaceAll("[^A-Za-z].*", ""))
.filter(s -> !s.isEmpty())
.collect(Collectors.joining(" "));
I have a situation where I want to get separator char from the given string like as below :-
String str1 = "saurabh|om|anurag|abhishek|jitendra"
String str2 = "amit,ankur,sumit,aniket,suheel"
String str3 = "aj-kumar-manav-lalit-gaurav"
-------
In above strings I want to get separator char as :-
String separatorStr1 = "|"
String separatorStr2 = ","
String separatorStr3 = "-"
Note :- separator char always will be non-alphabetical in string
Is there any way to achieve this.
Using groovy regexp and find ([^\w] is any non-alphanumeric character)
def getSeparator = { str ->
str.find(~/[^\w]/)
}
String str1 = "saurabh|om|anurag|abhishek|jitendra"
String str2 = "amit,ankur,sumit,aniket,suheel"
String str3 = "aj-kumar-manav-lalit-gaurav"
assert getSeparator(str1) == '|'
assert getSeparator(str2) == ','
assert getSeparator(str3) == '-'
Why is a - separator of str3? It could be a as well.
Assuming separator must be non-alphabetical loop through characters and look for first non-alphabetical character.
In future questions try to avoid other users guessing what you mean - try to define the subject of a topic.
By xenteros suggestion I have achieved this by following way :-
String str1 = "saurabh|om|anurag|abhishek|jitendra"
String str2 = "amit,ankur,sumit,aniket,suheel"
String str3 = "aj-kumar-manav-lalit-gaurav"
String separatorStr1 = str1.toCharArray().find { !Character.isLetterOrDigit(it) }
String separatorStr2 = str2.toCharArray().find { !Character.isLetterOrDigit(it) }
String separatorStr3 = str3.toCharArray().find { !Character.isLetterOrDigit(it) }
assert separatorStr1 == '|'
assert separatorStr2 == ','
assert separatorStr3 == '-'
I want to place a dash after every letter but my regex place a dash at the end too. How can I improve my regex?
String outputS = dnaString.replaceAll("(.{1})", "$1-");
(.)(?!$)
You can use this.Replace by $1.See demo.
https://regex101.com/r/gT6vU5/11
(?!$) uses negative lookahead to state that do not capture a character which is at end of string.
Without regex (that is faster):
String[] nucleotides = dnaString.split("");
String outputS;
int seqLength = nucleotides.length;
if (seqLength > 1) {
StringBuilder sb = new StringBuilder();
sb.append(nucleotides[0]);
for (int i = 1; i < seqLength; i++) {
sb.append("-");
sb.append(nucleotides[i]);
}
outputS = sb.toString();
} else {
outputS = dnaString;
}
I know this is an old question, but for completeness and future reference I would like to add this answer.
In Java 8 you can also use:
String.join("-",dnaString.toCharArray());
Explanation:
String.join(delimiter,objects...);
String.join(delimiter,array);
String.join(delimiter,Iterable);
These are used to join all objects to a single string with the delimiter as separator.
dnaString.toCharArray();
This is a method to get a String as an char array.
This replaces all special characters with underscore '_' except the last occurence of a special character in the string.
String name = "one-of-the dummy$ string:i.txt"; // input
name = name.replaceAll("[^a-zA-Z0-9](?=.*[^a-zA-Z0-9])", "_");
System.out.println(name);
//input: one-of-the dummy$ string:i.txt
//output: one_of_the_dummy__string_i.txt
This
(.)\B
doesn't match the last char.
See https://regex101.com/r/p0Z0zA/1
So, in your case, should be:
String outputS = dnaString.replaceAll("(.{1})\\B", "$1-");
Credits to pigreco.
What is the best way to produce a highlighted string found within another string?
I want to ignore all character that are not alphanumeric but retain them in the final output.
So for example a search for 'PC3000' in the following 3 strings would give the following results:
ZxPc 3000L = Zx<font color='red'>Pc 3000</font>L
ZXP-C300-0Y = ZX<font color='red'>P-C300-0</font>Y
Pc3 000 = <font color='red'>Pc3 000</font>
I have the following code but the only way i can highlight the search within the result is to remove all the whitespace and non alphanumeric characters and then set both strings to lowercase. I'm stuck!
public string Highlight(string Search_Str, string InputTxt)
{
// Setup the regular expression and add the Or operator.
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
// Highlight keywords by calling the delegate each time a keyword is found.
string Lightup = RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));
if (Lightup == InputTxt)
{
Regex RegExp2 = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
RegExp2.Replace(" ", "");
Lightup = RegExp2.Replace(InputTxt.Replace(" ", ""), new MatchEvaluator(ReplaceKeyWords));
int Found = Lightup.IndexOf("<font color='red'>");
if (Found == -1)
{
Lightup = InputTxt;
}
}
RegExp = null;
return Lightup;
}
public string ReplaceKeyWords(Match m)
{
return "<font color='red'>" + m.Value + "</font>";
}
Thanks guys!
Alter your search string by inserting an optional non-alphanumeric character class ([^a-z0-9]?) between each character. Instead of PC3000 use
P[^a-z0-9]?C[^a-z0-9]?3[^a-z0-9]?0[^a-z0-9]?0[^a-z0-9]?0
This matches Pc 3000, P-C300-0 and Pc3 000.
One way to do this would be to create a version of the input string that only contains alphanumerics and a lookup array that maps character positions from the new string to the original input. Then search the alphanumeric-only version for the keyword(s) and use the lookup to map the match positions back to the original input string.
Pseudo-code for building the lookup array:
cleanInput = "";
lookup = [];
lookupIndex = 0;
for ( index = 0; index < input.length; index++ ) {
if ( isAlphaNumeric(input[index]) {
cleanInput += input[index];
lookup[lookupIndex] = index;
lookupIndex++;
}
}
I wondering how should be the regex string for the string containig '#'
e.g.
abc#def#ghj#ijk
I wanna get
#def
#ghj
#ijk
I tried #[\S]+ but it selects the whole #def#ghj#ijk Any ideas ?
Edit
The code below selects only #Me instead of #MessageBox. Why ?
var m = new RegExp('#[^\s#]+').exec('http://localhost/Lorem/10#MessageBox');
if (m != null) {
var s = '';
for (i = 0; i < m.length; i++) {
s = s + m[i] + "\n";
}
}
Edit 2
the double backslash solved that problem. '#[^\\s#]+'
Try #[^\s#]+ to match # followed by a sequence of one or mor characters which are neither # nor whitespace.
Match all characters that are not #:
#[^#]+