?
Solved

Search StringBuilder for character and replace

Posted on 2008-02-03
9
Medium Priority
?
258 Views
Last Modified: 2013-11-23
Hi,

I am using a stringbuilder object, I want to search the contents of the stringbuilder and replace a lowercase 'z' with an 's', here is what I have so far:

 for(int i = 0; i <= sb.length(); i++)
         {
            if(sb.charAt(i) == "z")
            {
              sb.insert(i,'s');
            }
         }
This doesn't work as I can't compare a char with a string any ideas how i could modify this so it works?

Thanks
0
Comment
Question by:sjsharp80
  • 5
  • 4
9 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20808930
0
 

Author Comment

by:sjsharp80
ID: 20808943
Can you give an example?

   
0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 1600 total points
ID: 20808951
for(int i = 0; i <= sb.length(); i++)
         {
            if(sb.charAt(i) == 'z' )
            {
              sb.setCharAt(i, 's');
              }
         }
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:sjsharp80
ID: 20809103
This gives me and error when trying to complile: incomparable types char and string
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20809120
> incomparable types char and string

where? Are you sure you are using same code?
0
 

Author Comment

by:sjsharp80
ID: 20809200
This line:

            if(sb.charAt(i) == 'z' )
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20811625
Are you sure you are using exactly same line? it works for me.
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 20811628
be sure that its 'z' not "z"
0
 

Author Comment

by:sjsharp80
ID: 20813578
Ah, didn't spot that, it now works, many thanks.
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month3 days, 23 hours left to enroll

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question