| Author |
seeing id string has odd / even chars
|
dale con
Ranch Hand
Joined: Apr 15, 2005
Posts: 93
|
|
hi all i can find the length of a string String s = "mystringodd"; can someone show me how i can then see if the string conains an even number of characters e.g String s contains 11 chars whati want to do is see if String s is even, if not i need to append a 0 to make it even, so string s becomes this 0mystringodd can anyone help me ou with this many thanks
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
If you have the length, look at the % arithmetic operator. That will give you the remainder from a division which you can use to see if length is even or not. If you're still stuck, post some code and we'll go from there.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
dale con
Ranch Hand
Joined: Apr 15, 2005
Posts: 93
|
|
thanks i can get the remainder doing this String s = "somestringvalue"; int n = s.length(); int remainder = n % 2; but don't know how append String s with x number of 0's to make the String s even
|
 |
Seb Mathe
Ranch Hand
Joined: Sep 28, 2005
Posts: 225
|
|
if your length is odd, and then you add one char, your new length becomes even, no ? [ October 18, 2005: Message edited by: Seb Mathe ]
|
Regards,<br />Seb<br /> <br />SCJP 1.4
|
 |
Joel McNary
Bartender
Joined: Aug 20, 2001
Posts: 1815
|
|
Are you appending or prepending? In either case, you have two basic choices: String concatenation or a StringBuffer. String concatenation is simpler and, in this case, is more readable. Note that Strings are immutable -- you can't change them, you can only create new strings based on them. (Of course, you can reassign a reference to point to a new String; you can say s = "0" + s;). The StringBuffer alternative is best used when you are doing large blocks of concatenation. [ October 18, 2005: Message edited by: Joel McNary ]
|
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
|
 |
 |
|
|
subject: seeing id string has odd / even chars
|
|
|