This is very odd as I have used the Replace function for thousands of times. This is my code:
while (d.IndexOf("--") != -1) d=d.Replace("--", "-");
and this is the variable d's value when I trace:
"آدنیس,اسم دختر,girl name,آدونیس---گلی-به-رنگ-زرد-و-قرمز-که-فقط-هنگام-تابش-خورشید-باز-می-شود"
but it stuck when the value of d is:
"آدنیس,اسم دختر,girl name,آدونیس--گلی-به-رنگ-زرد-و-قرمز-که-فقط-هنگام-تابش-خورشید-باز-می-شود"
can anybody tell me why? Its funny that even dashes are added programatically.
You are facing this issue because you are using the replace method incorrectly. When you call the replace method on a string in python you get a new string with the contents replaced as specified in the method call. You are not storing the modified string but are just using the unmodified string.
The "replace is not a function" error occurs when we call the replace() method on a value that is not of type string . To solve the error, convert the value to a string using the toString() method before calling the replace() method. Here is an example of how the error occurs.
Like most text editors and word processors, you can find a specific string of text and replace it with another string of text. Choose the Replace option from the Edit menu or press CTRL+H to display the Replace dialog.
The String type provides you with the replace() and replaceAll() methods that allow you to replace all occurrences of a substring in a string and return the new version of the string.
That is because this:
var d1 = "آدنیس,اسم دختر,girl name,آدونیس---گلی-به-رنگ-زرد-و-قرمز-که-فقط-هنگام-تابش-خورشید-باز-می-شود";
is not the same as this:
var d2 = "آدنیس,اسم دختر,girl name,آدونیس---گلی-به-رنگ-زرد-و-قرمز-که-فقط-هنگام-تابش-خورشید-باز-می-شود";
The last three characters in your string are not actually the unicode -
Try it yourself:
var d1 = "آدنیس,اسم دختر,girl name,آدونیس---گلی-به-رنگ-زرد-و-قرمز-که-فقط-هنگام-تابش-خورشید-باز-می-شود";
var d2 = "آدنیس,اسم دختر,girl name,آدونیس---گلی-به-رنگ-زرد-و-قرمز-که-فقط-هنگام-تابش-خورشید-باز-می-شود";
while (d.IndexOf("--", StringComparison.Ordinal) != -1) d1 = d1.Replace("--", "-");
Console.WriteLine(d1); // the last characters are left
while (d2.IndexOf("--", StringComparison.Ordinal) != -1) d2 = d2.Replace("--", "-");
Console.WriteLine(d2); // All clear
Just FYI: String comparison method indexof is culture specific. I would use:
var d = "آدنیس,اسم دختر,girl name,آدونیس---گلی-به-رنگ-زرد-و-قرمز-که-فقط-هنگام-تابش-خورشید-باز-می-شود";
while (d.IndexOf("--", System.StringComparison.Ordinal) != -1)
d = d.Replace("--", "-");
Since it uses ordinal rules i.e. culture independent unicode values, and it runs faster.
You can use Regex.Replace()
string _txt = "----------";
_txt = Regex.Replace(_txt, @"\-{2,}", "-");
this will output: -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With