Reputation: 1676
I am trying to replace only one character in a string dart but can not find any efficient way of doing that. As string is not array in Dart I can't access the character directly by index and there is no function coming in-built which can do that. What is the efficient way of doing that?
Currently I am doing that like below:
List<String> bedStatus = currentBedStatus.split("");
bedStatus[index]='1';
String bedStatusFinal="";
for(int i=0;i<bedStatus.length;i++){
bedStatusFinal+=bedStatus[i];
}
}
index is an int and currentBedStatus is the string I am trying to manipulate.
Upvotes: 40
Views: 53654
Reputation: 1969
You can use String.replaceRange to replace a single character of a string given a start and end index.
The method declaration looks like this:
String replaceRange(int start, int? end, String replacement)
So given the string "hello"
and you'd want to replace "e"
with "a"
, call:
"hello".replaceRange(1, 2, "a")
So 1
is the index at "e"
and 2
is the index after that and the result is "hallo"
.
You could make a function to replace a single character like this:
String replaceCharAt(String text, int index, String replacement) {
return text.replaceRange(index, index + 1, replacement);
}
Upvotes: 3
Reputation: 188
You can also use substrings and not worry about chars. Create substrings from string and use indices.
String ourString = "OK_AY"; // original string - length of 5
String oldChar = "_"; // the character we wanna find and replace - as type string - in this case, an underscore
String newChar = "X" // the char we wanna replace underscore with
int index = ourString.indexOf(oldChar, 0); // find the index in string where our char exists (may not exist)
//some logic here to return or skip next code if char wasn't found
//oldChar found at index 0 (beginning of string)
if (index == 0) {
ourString = newChar + ourString.substring(index+1, ourString.length);
}
//oldChar found at index 4 (end of string)
else if (index == str.length-1) {
ourString = ourString.substring(0, index) + newChar;
}
//oldChar found anywhere else between
else {
ourString = ourString.substring(0, index) + newChar + ourString.substring(index+1, ourString.length);
}
//done, ourString is now updated "OKXAY"
Upvotes: 0
Reputation: 518
Here is what you can do
final singleChar = 'a';
final characters = yourString.characters.toList();
characters[index] = singleChar;
yourString = characters.join('');
Here is how it would look like in a String extension method
String replaceCharAt({required String char, required int index}) {
final chars = characters.toList();
chars[index] = char;
return chars.join('');
}
Upvotes: 1
Reputation: 51
with this line you can replace the $
symbol with a blank space ''
'${double.parse (_priced.toString (). replaceAll (' \ $ ',' ')) ?? '\ $ 0.00'}'
String x = _with.price.toString (). ReplaceAll ('\ $', '')) ?? '\ $ 0.00',
Upvotes: 5
Reputation: 71
You can just use this in build function to do it.
s.replaceRange(start, end, newString)
Upvotes: 3
Reputation: 19
You can use this function, just modified Dinesh's answer
String _replaceCharAt(
{required String character,
required int index,
required String oldString}) {
if (oldString.isEmpty) {
return character;
} else if (index == oldString.length) {
return oldString.substring(0, index) + character;
} else if (index > oldString.length) {
throw RangeError('index value is out of range');
}
return oldString.substring(0, index) +
character +
oldString.substring(index + 1);
}
Upvotes: 0
Reputation: 553
You can use replaceAll().
String string = 'string';
final letter='i';
final newLetter='a';
string = string.replaceAll(letter, newLetter); // strang
Upvotes: 23
Reputation: 511626
You can use replaceFirst()
.
final myString = 'hello hello';
final replaced = myString.replaceFirst(RegExp('e'), '*'); // h*llo hello
Or if you don't want to replace the first one you can use a start index:
final myString = 'hello hello';
final startIndex = 2;
final replaced = myString.replaceFirst(RegExp('e'), '*', startIndex); // hello h*llo
Upvotes: 23
Reputation: 21728
Replace at particular index:
As String
in dart is immutable refer, we cannot edit something like
stringInstance.setCharAt(index, newChar)
Efficient way to meet the requirement would be:
String hello = "hello";
String hEllo = hello.substring(0, 1) + "E" + hello.substring(2);
print(hEllo); // prints hEllo
Moving into a function:
String replaceCharAt(String oldString, int index, String newChar) {
return oldString.substring(0, index) + newChar + oldString.substring(index + 1);
}
replaceCharAt("hello", 1, "E") //usage
Note: index
in the above function is zero based.
Upvotes: 31