This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding query about string

 
shweta pandit
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 10427
63
Eclipse IDE Hibernate Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10427
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49411
62
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10225
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you can always search on Google to get some inspiration...
 
shweta pandit
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
atlast i did it......
here is the solution:


thanks everyone for suggestions......
 
Roel De Nijs
Sheriff
Posts: 10225
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following solution works fine



 
Roel De Nijs
Sheriff
Posts: 10225
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using Character.isLowerCase and Character.isUpperCase is a huge improvement. But what about a String with 10 characters?
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10225
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10225
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10225
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10225
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10225
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
1
Java Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
swapCase("Oops... I didn't try that.") returns oOPS... i DIDN'T TRY THAT.

That's surprising..
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic