UrbanPro

Learn Java Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Write a programme to replace the string without using replace command? This is my string. ..... s is replaced by th output is Thith ith my thtring

Asked by Last Modified  

Follow 0
Answer

Please enter your answer

/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package logic; import java.util.*; /** * * @author AP Edusoft */ public class Logic { /** ...
read more
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package logic; import java.util.*; /** * * @author AP Edusoft */ public class Logic { /** * @param args the command line arguments */ public static void main(String[] args) { String originalString = "This is the cat"; String replacedString = replaceMethod(originalString, "the", "your"); System.out.println(replacedString); } static String replaceMethod(String str, String from, String to) { String[] arr = str.split(from); StringBuilder output = new StringBuilder(); int i = 0; for (; i < arr.length - 1; i++) output.append(arr[i]).append(to); output.append(arr[i]); if (str.substring(str.lastIndexOf(" ")).equalsIgnoreCase(" " + from)) output.append(to); return output.toString(); } } read less
Comments

Java Professional with 11+ Yrs of experience

Reverse String Method 1: 1. The user will input the string to be reversed. 2. First we will convert String to character array by using the built in java String class method toCharArray(). 3. Then , we will scan the string from end to start, and print the character one by one. Reverse String...
read more
Reverse String Method 1: 1. The user will input the string to be reversed. 2. First we will convert String to character array by using the built in java String class method toCharArray(). 3. Then , we will scan the string from end to start, and print the character one by one. Reverse String Method 2: 1. In the second method , we will use the built in reverse() method of the StringBuilder class ,. Note : String class does not have reverse() method . So we need to convert the input string to StringBuilder , which is achieved by using the append method of the StringBuilder. 2. After that print out the characters of the reversed string by scanning from the first till the last index. Reverse String Method 3: 1. Convert the input string into character array by using the toCharArray() built in method of the String Class . 2. In this method we will scan the character array from both sides , that is from the start index (left) as well as from last index(right) simultaneously. 3. Set the left index equal to 0 and right index equal to the length of the string -1. 4. Swap the characters of the start index scanning with the last index scanning one by one .After that increase the left index by 1 (left++) and decrease the right by 1 i.e (right--) to move on to the next characters in the character array . 5. Continue till left is less than or equal to the right . Reverse String Method 4: 1. Convert the input string into the character array by using toCharArray() built in method. reverse a string in java with example 2. Then add the characters of the array into the LinkedList object . We used LinkedList because it maintains the insertion order of the input values. 3. Java also has built in reverse() method for the Collections class . Since Collections class reverse() method takes a list object , to reverse the list , we will pass the LinkedList object which is a type of list of characters. 4. We will create the ListIterator object by using the listIterator() method on the LinkedList object. ListIterator object is used to iterate over the list. 5. ListIterator object will help us to iterate over the reversed list and print it one by one to the output screen. Reverse String Method 5 : 1. The last method is converting string into bytes . getBytes() method is used to convert the input string into bytes[]. 2. Then we will create a temporary byte[] of length equal to the length of the input string. 3. We will store the bytes(which we get by using getBytes() method) in reverse order into the temporary byte[] . read less
Comments

completeeducationhub.com web site you will get information

String str="This is my String"; String s=" "; for(int i=0;i
Comments

completeeducationhub.com web site you will get information

for(i=0;i
Comments

Java Technical Leader

Look at the program. If you face problem to understand this coding let me know. package com.test.scjp; public class StringReplacer { public String replaceMethod(String original, String toReplace, String replacedWith) { for(int j=0;j read more
Look at the program. If you face problem to understand this coding let me know. package com.test.scjp; public class StringReplacer { public String replaceMethod(String original, String toReplace, String replacedWith) { for(int j=0;jread less
Comments

Software Engineer

I have done this in javascript . i think u just only need to replace the var with string . var myStr = "Thith ith my thtring"; var newStr = ""; for(var i=0;i
Comments

Software Engineer

I have done this in javascript . i think u just only need to replace the var with string . var myStr = "Thith ith my thtring"; var newStr = ""; for(var i=0;i
Comments

Java Tutor

It can be done using split/substring method..
Comments

Computer science tutor

String str="This is my String"; StringBuffer buffer=new StringBuffer(); for(int i=0;i read more
String str="This is my String"; StringBuffer buffer=new StringBuffer(); for(int i=0;iread less
Comments

MCA, BIT Mesra, Ranchi

Apart from all the basic ways, this can be done with the help of pattern(regular expressions) which in-turn applies String class replaceAll method: public static String replaceString(String sourceStr, String replaceString, String replaceBy) { return Pattern.compile(replaceString).matcher(sourceStr).replaceAll(replaceBy); } public...
read more
Apart from all the basic ways, this can be done with the help of pattern(regular expressions) which in-turn applies String class replaceAll method: public static String replaceString(String sourceStr, String replaceString, String replaceBy) { return Pattern.compile(replaceString).matcher(sourceStr).replaceAll(replaceBy); } public static void main(String[] args) { System.out.println(replaceString("This is my string", "s", "th")); } Output: Thith ith my thtring read less
Comments

View 12 more Answers

Related Questions

As a fresher I have done Python training, but hardly getting response from any company. What should I do?
only having training in Python is not enough to be called for intervew. Better you should get real time certification course form reputed organisation with smart hand-on real project exposure. Try to have...
Yamini
0 0
6
What are the booming technologies in US from Job point of view
All the terminologies are good. First of all decide your self you want go as functional consultant or technical. If you have java background Learn Hadoop. Billing rate is very high.
Chandra

What is meant by Java full stack and angular Java?

Fullstack development means the combination of UI technologies (client-side) + Backend technology (server-side). UI technology like Angular or ReactJs (Front end technologies). It's all about client-side...
Anuradha
What is the difference between abstraction and encapsulation?
Encapsulation is wrapping, just hiding properties and methods. Encapsulation is used for hide the code and data in a single unit to protect the data from the outside the world. Class is the best example...
Neval
What are the best websites to learn java as well as get a certificate from them?
To learn basic java i suggest javatpoint is good for knowledge as well as concept but for certification refer book
Abirami
0 0
7

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Facebook Analytics
Assume how the Facebook application will store the millions of customer's record in real-time: facebook = { 'jose': { 'name': 'jose', 'age': 33, 'hobby': , # cricket,football 'mobile': 1111111111, 'email':...

Hibernate 5
The Hibernate team published the first Hibernate 5 release quite a while ago, and since then they introduced a bunch of new features. I explained several of them here on the blog, and it’s about...
M

Mohammed Shahnawaz Akhter

2 0
0

Try to clear up the basics, if basics are clear then you can go ahead with any difficult problem
Hey guys, To all the students i just want to convey that just clear up your basics so that they can help you solve anyu problem and you would achieve a great success. Regards, Ishani Chakraborty

Software Development Training In Jaipur
Satyam Web Solution provides website designing &development and software designing &development training in Jaipur for various stream’s students. MCA 6 month Industrial Training/Internship B....

Basic Concepts of Web Designing
An introduction to domain names, web servers, and website hosting 1)What is the web? In a nutshell, the web is a whole bunch of interconnected computers talking to one another. The computers (on the...

Recommended Articles

Designed in a flexible and user-friendly demeanor, Java is the most commonly used programming language for the creation of web applications and platform. It allows developers to “write once, run anywhere” (WORA). It is general-purpose, a high-level programming language developed by Sun Microsystem. Initially known as an...

Read full article >

In the domain of Information Technology, there is always a lot to learn and implement. However, some technologies have a relatively higher demand than the rest of the others. So here are some popular IT courses for the present and upcoming future: Cloud Computing Cloud Computing is a computing technique which is used...

Read full article >

Java is the most famous programming language till date. 20 years is a big time for any programming language to survive and gain strength. Java has been proved to be one of the most reliable programming languages for networked computers. source:techcentral.com Java was developed to pertain over the Internet. Over...

Read full article >

Before we start on the importance of learning JavaScript, let’s start with a short introduction on the topic. JavaScript is the most popular programming language in the world, precisely it is the language - for Computers, the Web, Servers, Smart Phone, Laptops, Mobiles, Tablets and more. And if you are a beginner or planning...

Read full article >

Looking for Java Training Classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Java Training Classes?

The best tutors for Java Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Java Training with the Best Tutors

The best Tutors for Java Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more