• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to find and replace a substring from a given string

 
armaan lovras
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone

I want to know that how can we find a substring in a given string without using string functions, although we can use length() function.

It can be easily done in C/C++ because in these(C/C++) Strings are array of characters , but as you know in java we consider Strings as objects.

So please try to write a code for it using java .

For Example:-

we get a string HELLOINDIA , now we want to replace INDIA with WORLD , so that it becomes HELLOWORLD .


Also please explain how can we convert a given String into an array of characters .





Thank you

With Regards
Armaan Lovras
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a method in String which returns an array of characters for the String; there's also a method which returns the character at a given index. Look at the Javadoc for the java.lang.String class to find them.

Nothing you do can change an actual String object, by the way; all you can do is make a new String with a new value. Strings are immutable -- they have no methods which allow you to change the characters they contain.

In C++ a string is not just an array of characters, by the way; it's an object. A char* is, of course, just a pointer to some characters; but "string" is a kind of object in C++ just as in Java.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!