Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Changing Only A Character Within A String

Thread view: 
Jacky - 09 Mar 2004 19:46 GMT
Hi,

I was looking at replacing only a character withing a string by position,
not global replace:
http://dlt.ncsa.uiuc.edu/javadoc/grunk/ncsa/emerge/util/StringUtils.html

How shall I go about doing it?

Thanks,
Jacky
Steve W. Jackson - 09 Mar 2004 19:53 GMT
>:Hi,
>:
[quoted text clipped - 6 lines]
>:Thanks,
>:Jacky

You can't.  Remember, a String is immutable -- meaning it cannot be
changed.  But that's not the answer to your "actual" question, which is
how to get a new String with only the single character changed.

Do you know the actual position of the character in question?  If so,
look to the charAt(int) method.  It's zero-based, naturally, so the
number should range from zero to length()-1.  If you don't know the
actual position, you can get it with indexOf(char) that returns int.  In
either case, you can create the new string with combinations of those
methods and substring.

HTH.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Jacky - 09 Mar 2004 20:31 GMT
> Do you know the actual position of the character in question?  If so,
> look to the charAt(int) method.  It's zero-based, naturally, so the
> number should range from zero to length()-1.  If you don't know the
> actual position, you can get it with indexOf(char) that returns int.  In
> either case, you can create the new string with combinations of those
> methods and substring.

Yupe, the actual position of the character is known. But if it's in the
middle, u have right and left leftovers?

Example:
String test = "0123456789";
and I want to change "5" into "a", like "01234a6789";

How do you take care of it?

I was thinking of keeping the "01234" and "6789" as 2 separate strings, and
adding them together by

test = "01234" + "a" + "6789";

But I find that it's a bit clumsy......any better ways?........
RC - 09 Mar 2004 21:24 GMT
> Yupe, the actual position of the character is known. But if it's in the
> middle, u have right and left leftovers?
[quoted text clipped - 11 lines]
>
> But I find that it's a bit clumsy......any better ways?........

You can do

char[] c = test.toCharArray();
c[5] = 'a';
String s = new String(c);
test = s;
Steve W. Jackson - 10 Mar 2004 17:41 GMT
>:> Do you know the actual position of the character in question?  If so,
>:> look to the charAt(int) method.  It's zero-based, naturally, so the
[quoted text clipped - 18 lines]
>:
>:But I find that it's a bit clumsy......any better ways?........

As the other replies have indicated, there are numerous ways to go about
it.  Just keep in mind that creating too many String objects can get
dangerous (depending on the nature of the overall code involved), since
each "change" of a String actually leaves an old String object available
for garbage collection and creates another new one.  for instance, your
example above demonstrates a "bad way" to do it if used excessively.  
You've got 3 literal String objects.  In Sun's code, if not optimized
out, that would create a StringBuffer object with the "01234" part, then
append() the "a" to it, and append "6789" to that, and call its
toString() method to create the final String object with the
concatenated result.  This isn't necessarily always universally bad --
just not something that you want to get into doing in excess, in tight
loops, in performance sensitive code, etc., etc., etc.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

S Yanoff - 10 Mar 2004 05:01 GMT
> Hi,
>
[quoted text clipped - 3 lines]
>
> How shall I go about doing it?

If it is acceptable to you to replace just the first occurence, you can
use String.replaceFirst();

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#replaceFirst(java.
lang.String,%20java.lang.String
)

Good luck,
-Scott
Jacky - 10 Mar 2004 21:09 GMT
> If it is acceptable to you to replace just the first occurence, you can
> use String.replaceFirst();

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#replaceFirst(java.
lang.String,%20java.lang.String
)

I've seen that, I'm surprised that there's an option to replace the 1st
occurance, or to replace all with the String.Replace command, and there
isn't a method that do a String.ReplaceAt some position method advailable.

Guess I will be churning out my code in arrays of char? so that I can easily
change the xth position?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.