Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 306 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to replace only one character in a string in Dart?

#1
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.
Reply

#2
**Replace at particular index:**

As `String` in dart is **immutable** [refer][1], 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*.


[1]:

[To see links please register here]

Reply

#3
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
Reply

#4

You can use **replaceAll().**
```
String string = 'string';
final letter='i';
final newLetter='a';
string = string.replaceAll(letter, newLetter); // strang
```
Reply

#5
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);
}
```
Reply

#6
You can just use this in build function to do it.

s.replaceRange(start, end, newString)
Reply

#7
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',
```
Reply

#8
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('');
}
```
Reply

#9
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"
Reply

#10
You can use [String.replaceRange](

[To see links please register here]

) to replace a single character of a string given a **start** and **end** index.

The method declaration looks like this:
```Dart
String replaceRange(int start, int? end, String replacement)
```

So given the string `"hello"` and you'd want to replace `"e"` with `"a"`, call:
```Dart
"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:

```Dart
String replaceCharAt(String string, int index, String replacement) {
return string.replaceRange(index, index + 1, replacement);
}
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through