my dog learned polymorphism*
The moose likes Java in General and the fly likes regarding query about string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "regarding query about string" Watch "regarding query about string" New topic
Author

regarding query about string

shweta pandit
Greenhorn

Joined: Sep 05, 2012
Posts: 3

i hava a string with lowercase and uppercase letters how can i convert the lowercase letters into uppercase and viceversa.please reply soon
thanks in advance...........
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

shweta pandit wrote:i hava a string with lowercase and uppercase letters how can i convert the lowercase letters into uppercase and viceversa.please reply soon
thanks in advance...........

Have a look at at the API for java.lang.String (←click). I suspect a couple of methods will soon leap out at you.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Welcome to the Ranch

If you are converting both ways, so JavaRanch becaomes jAVArANCH, you will need more than String. There are several ways to do it, but start by getting the String into char[] format with a String class method.
Prashant He
Greenhorn

Joined: Sep 05, 2012
Posts: 4

Use the following logic:

1. check if the charecter is in uppercase or not ...then acoordingly you can change it


eg
String s="AbcdeF";

for (int i=0;i<s.length();i++){
char c=s.charAt(i))
char c1=null;
if(Character.isUpperCase(c){
c1 =Charecter.toLowerCase(c);
}else if (Character.isLowerCase(c)){
c1 =Charecter.toLowerCase(c);
}
c=c1;
}


Hope this helps.
shweta pandit
Greenhorn

Joined: Sep 05, 2012
Posts: 3
shweta pandit wrote:
i hava a string with lowercase and uppercase letters how can i convert the lowercase letters into uppercase and viceversa.please reply soon
thanks in advance...........

problem is not solved please reply....
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

shweta pandit wrote:problem is not solved please reply....

Shweta.

First: EaseUp (←click). We're all volunteers here.

Second: I suggest you read the guide HowToAskQuestionsOnJavaRanch. This site is NotACodeMill, and we expect you to ShowSomeEffort; and part of that involves, as I said in my first post, taking the time to familiarize yourself with the API.

Winston
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Look, this site isn't about handing out full answers, it's about helping you learn. Show us what you've tried so far, and tell us what you don't understand. We're happy to help you find your own solution, but you need to ShowSomeEffort.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Welcome to the Ranch
Prashant He wrote: . . . Hope this helps.
No, it doesn’t I am afraid.
One, because we do not give out complete solutions.
Two, because there is a major flaw in your understanding of how Strings work and what the char c is.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

And you can always search on Google to get some inspiration...


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
shweta pandit
Greenhorn

Joined: Sep 05, 2012
Posts: 3
atlast i did it......
here is the solution:


thanks everyone for suggestions......
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

That solution can use some optimization. Have a look at some methods of the Character class to begin with.

And when posting actual (java) code snippets, please UseCodeTags. It improves readibility.
Prashant He
Greenhorn

Joined: Sep 05, 2012
Posts: 4
The following solution works fine



Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

Using Character.isLowerCase and Character.isUpperCase is a huge improvement. But what about a String with 10 characters?
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

Prashant He wrote:The following solution works fine





Prashant, read the Campbell's last post.. This wouldn't help as you are posting direct answers..
We don't do it here.. We are NotACodeMill
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

Roel De Nijs wrote:Using Character.isLowerCase and Character.isUpperCase is a huge improvement. But what about a String with 10 characters?

Well, as far as I know, there is nothing available in String class to handle such a problem(As of today).. One has no other choice rather than to handle this one..
And yes this is not a good way, but what else can be done??
Any Idea??
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

R. Jain wrote:And yes this is not a good way, but what else can be done??
Any Idea??

Yes! This solution will only work for a String of 6 characters and maybe there's no need to use an extra array, maybe one can use a class from the Java API which main purpose is modifications on a character sequence.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

R. Jain wrote:
Roel De Nijs wrote:Using Character.isLowerCase and Character.isUpperCase is a huge improvement. But what about a String with 10 characters?

Well, as far as I know, there is nothing available in String class to handle such a problem(As of today).. One has no other choice rather than to handle this one..
And yes this is not a good way, but what else can be done??
Any Idea??


A better way to do this can be (By Toggling each character) : -

1). Convert the String into byteArray..
2). Iterate over byteArray and for each element do an XOR with 32
3). Create new byteArray with new element.. [Edit: Update the existing byteArray with toggled element]
4). byteArray back to String..
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

R. Jain wrote:A better way to do this can be (By Toggling each character) : -

1). Convert the String into byteArray..
2). Iterate over byteArray and for each element do an XOR with 32
3). Create new byteArray with new element.. [Edit: Update the existing byteArray with toggled element]
4). byteArray back to String..

That depends on your understanding of "better" (more performant, more maintainable,...). I would definitely not opt for this solution, because it might be slightly more performant, but bit twiddling is harder to read and maintain than a simple Character.toUpperCase(c)
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

R. Jain wrote:
R. Jain wrote:
Roel De Nijs wrote:Using Character.isLowerCase and Character.isUpperCase is a huge improvement. But what about a String with 10 characters?

Well, as far as I know, there is nothing available in String class to handle such a problem(As of today).. One has no other choice rather than to handle this one..
And yes this is not a good way, but what else can be done??
Any Idea??


A better way to do this can be (By Toggling each character) : -

1). Convert the String into byteArray..
2). Iterate over byteArray and for each element do an XOR with 32
3). Create new byteArray with new element.. [Edit: Update the existing byteArray with toggled element]
4). byteArray back to String..


ASCII Code for A-> 65 (01000001)
Bit value for 32 -> (00100000)
XOR THEM
ASCII Code for a -> 97 (01100001)
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

Roel De Nijs wrote:
because it might be slightly more performant, but bit twiddling is harder to read and maintain than a simple Character.toUpperCase(c)

That depends upon what is needed.. This is just another solution.
And as far as bit twiddling is concerned, yeah it is harder to read, but it just needs one line of XORing to get it done. (And this is not much of a twiddling as I see)
Can be better trade-off than that character array.. Where you need to use an extra if-else to check for each character and convert them accordingly.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

R. Jain wrote:Can be better trade-off than that character array.. Where you need to use an extra if-else to check for each character and convert them accordingly.

The character array is not needed. I guess you want to XOR only alphabetic characters, so you still need an if.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

That if will also be needed when you consider only alphabetic characters in char array too... I'm sure you don't want to do a toLowerCase on a space...
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

R. Jain wrote: That if will also be needed when you consider only alphabetic characters in char array too... I'm sure you don't want to do a toLowerCase on a space...

Invoking toLowerCase on a space will return a space, so you don't need an if for that one. You do need an if to invert the character case (upper to lower, lower to upper)
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

Roel De Nijs wrote:
R. Jain wrote: That if will also be needed when you consider only alphabetic characters in char array too... I'm sure you don't want to do a toLowerCase on a space...

Invoking toLowerCase on a space will return a space, so you don't need an if for that one. You do need an if to invert the character case (upper to lower, lower to upper)

Oops... I didn't try that.
And there wins the toCharArray
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5147
    
  12

R. Jain wrote:And there wins the toCharArray

I would indeed opt for this solution:


swapCase("Oops... I didn't try that.") returns oOPS... i DIDN'T TRY THAT.

Note 1: an alternative could be to use a StringBuilder instead of the char array
Note 2: this solution ignores locale-specific rules when changing case (e.g. the upper case of "ß" is "SS" for German locales) (and if you need to honour these rules, you have to use a StringBuilder)
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 370
    
    1

Roel De Nijs wrote:
swapCase("Oops... I didn't try that.") returns oOPS... i DIDN'T TRY THAT.

That's surprising..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
R. Jain wrote: . . . Prashant, read the Campbell's last post.. This wouldn't help as you are posting direct answers..
We don't do it here.. We are NotACodeMill
Thank you for noticing. I would, however, leave that post, because it is a poor solution, and discussing its shortcomings will allow everybody to learn.

If it had been a good solution, I would have edited it to read
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

Roel De Nijs wrote:
R. Jain wrote:A better way to do this can be (By Toggling each character) : -

1). Convert the String into byteArray..
2). Iterate over byteArray and for each element do an XOR with 32
3). Create new byteArray with new element.. [Edit: Update the existing byteArray with toggled element]
4). byteArray back to String..

That depends on your understanding of "better" (more performant, more maintainable,...). I would definitely not opt for this solution, because it might be slightly more performant, but bit twiddling is harder to read and maintain than a simple Character.toUpperCase(c)


I read this solution and shuddered, "aggghhh bit twiddling" i thought "how horrible"
 
 
subject: regarding query about string
 
Similar Threads
String to Timestamp conversion
applet problem
regex matches
operator cannot be applied to ints.
searching for possible word