I am trying to create a string which substitutes all spaces for a * but I can't figure out exactly how to do that. Can anyone help?
String phrase = new String ("This is a String test.");
Python: Replace multiple characters in a string using the replace() In Python, the String class (Str) provides a method replace(old, new) to replace the sub-strings in a string. It replaces all the occurrences of the old sub-string with the new sub-string.
The Python replace() method is used to find and replace characters in a string. It requires a substring to be passed as an argument; the function finds and replaces it. The replace() method is commonly used in data cleaning.
replace() The replace() method returns a new string with one, some, or all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function called for each match. If pattern is a string, only the first occurrence will be replaced.
Java String replace() Method The replace() method searches a string for a specified character, and returns a new string where the specified character(s) are replaced.
Mystring = Mystring.Replace(' ', '*');
String phrase = new String ("This is a String test.");
/*Replace the Spaces with the *, */
String finalString = phrase.Replace(' ', '*');
System.out.println(finalString);
Assuming it's Java, you can use the String.replace method:
phrase = phrase.replace(' ', '*');
Do not create String with new operator. In Java, String is a special class. So
String phrase = "This is a String test.";
is enough. Creating String with new operator will create string twice.
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