Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

String replace

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a string which looks like this
Strinf s = " Hi java i am OK. Hello \n How are you";
I want to replace \n with a character like space ' ' or 'B'.
for e.g. s.replace('\n','B'); or s.replace('\n',''); .
this doesnt work
can anyone suggest any other way
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API docu:
"Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar."
That is, replace doesn't change the original String (Strings are immutable, after all), but *returns* a new String.
So, you have to write something like

Does that help?
 
author
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"haridave",
Thanks for participating here at the Ranch. However, the name you are using does not comply with our naming convention described at http://www.javaranch.com/name.jsp . Please log in with a new name, which meets these requirements.
You can change your name here.
Thanks.
Sean
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hari,
Hope you are looking for something like the following:
/**
* Method used to replace a source string with the destination string
*
* @param str the sourceString
* @param pattern the pattern to be replaced
* @param replace the string that replaces thepattern string
*
* @return replaced String
*/
public static String replace(String sourceString, String pattern, String replace) {
if (replace == null) {
replace = "";
}
int stringPosition = 0, stringIndex = 0;
StringBuffer result = new StringBuffer((int) sourceString.length() * 2);
while ((stringIndex = sourceString.indexOf(pattern, stringPosition)) >= 0) {
result.append(sourceString.substring(stringPosition, stringIndex));
result.append(replace);
stringPosition = stringIndex + pattern.length();
}
result.append(sourceString.substring(stringPosition));
return result.toString();
}
HTH,
Tom
 
You get good luck from rubbing the belly of a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!