String Replace Method Is Not Replacing Characters

Replace method is not replacing characters in a string

Just replace this line:

keyEdit.Replace('j', 'i');

with this:

keyEdit=keyEdit.Replace('j', 'i');

Returns a new string in which all occurrences of a specified string in
the current instance are replaced with another specified string. MSDN

String replace method is not replacing characters

And when I debug this the logic does fall into the sentence.replace.

Yes, and then you discard the return value.

Strings in Java are immutable - when you call replace, it doesn't change the contents of the existing string - it returns a new string with the modifications. So you want:

sentence = sentence.replace("and", " ");

This applies to all the methods in String (substring, toLowerCase etc). None of them change the contents of the string.

Note that you don't really need to do this in a condition - after all, if the sentence doesn't contain "and", it does no harm to perform the replacement:

String sentence = "Define, Measure, Analyze, Design and Verify";
sentence = sentence.replace("and", " ");

String replace function not replacing characters correctly - Java

A common mistake... You want:

    number = number.replace('8', '2');

String.replace() doesn't change the String, because Strings are immutable (they can not be changed). Instead, such methods return a new String with the calculated value.

Python string.replace() not replacing certain characters

Look at the function doc-string: string.replace(old, new, count)
so it replaces count as many occurences that it can find.

You cannot change a string. Each string is newly created, it is immutable. What you want is:

def replace_at_index(string, index):
string = string[:index] + "-" + string[index+1:]
return string

Python string.replace() not replacing characters

That's because filename and foldername get thrown away with each iteration of the loop. The .replace() method returns a string, but you're not saving the result anywhere.

You should use:

filename = line[2]
foldername = line[5]

for letter in bad_characters:
filename = filename.replace(letter, "_")
foldername = foldername.replace(letter, "_")

But I would do it using regex. It's cleaner and (likely) faster:

p = re.compile('[/:()<>|?*]|(\\\)')
filename = p.sub('_', line[2])
folder = p.sub('_', line[5])

replaceAll does not replace string

Change

query.replaceAll("REPLACEME", symbols.toString());

to:

query = query.replaceAll("REPLACEME", symbols.toString());

Strings in Java are designed to be immutable.

That is why replaceAll() can't replace the characters in the current string, so it must return a new string with the characters replaced.


Also if you want to simply replace literals and don't need regex syntax support use replace instead of replaceAll (regex syntax support is only difference between these two methods). It is safer in case you would want to replace literals which can contain regex metacharacters like *, +, [, ] and others.

Why does String.replace not work?

You did not assign it to test. Strings are immutable.

test = test.replace("KP", "");

You need to assign it back to test.

C# string replace does not actually replace the value in the string

The problem is that strings are immutable. The methods replace, substring, etc. do not change the string itself. They create a new string and replace it. So for the above code to be correct, it should be

path1 = path.Replace("\\bin\\Debug", "\\Resource\\People\\VisitingFaculty.txt");

Or just

path = path.Replace("\\bin\\Debug", "\\Resource\\People\\VisitingFaculty.txt");

if another variable is not needed.

This answer is also a reminder that strings are immutable. Any change you make to them will in fact create a new string. So keep that in mind with everything that involves strings, including memory management.
As stated in the documentation here.

String objects are immutable: they cannot be changed after they have
been created. All of the String methods and C# operators that appear
to modify a string actually return the results in a new string object

String.replace() is replacing at the wrong index even though the index is passed correctly

The problem is that in that case, you're basically doing this:

newStrB = "9999B".replace("9", "8");

That replaces the first "9" it finds, which is at the beginning.

In your question you spoke of "indexes" but you don't pass indexes to replace, you pass the string (or regular expression) to find and the string to replace. When you use a string for the first argument, only the first match (in this case, at the beginning of the string) is replaced. You used indexes to find the "9", but the one you found is identical to the first one (of course ), so replace replaces the wrong one.

Using indexes with substring would make more sense for this operation.

some issue in converting lowercase letter and replace function

You did not assign value to the variable after conversion. Please replace those 2 functions with

s1=s1.toLowerCase();
s3=s3.replace('b','a');


Related Topics



Leave a reply



Submit