aspose file tools*
The moose likes Beginning Java and the fly likes Accessing of character by character from a String... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing of character by character from a String..." Watch "Accessing of character by character from a String..." New topic
Author

Accessing of character by character from a String...

Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
hiii friends,
Since I'm very new to JAVA, I've lots of doubts in my mind. Even they are silly doubts for you , as a bigginner I wanna know the solution.

One of my doubts is, I've

String s1 = new String("abcd");
String s3 = new String();
int len = s1.length();

I can't use .......
for(int i=0;i<len;i++)
s3[i] = s2[i];


Then,
how to access character by character from the String s1 and place one by one into s3?( I know that s1 = s3 will do this job. But, I want to assign one by one to String s3).
>
Chuck Mondi
Ranch Hand

Joined: Oct 14, 2008
Posts: 31
Please use Code tag in your post.

After doing a little bit of research (because I was unfamiliar with doing this) I think I found a solution for you. The String class has a toCharArray() method that will place a String into a character array. I am still new too but hopefully this will help. You can use some of this logic to figure out how to load the characters into a new string variable s3.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

There are a couple of ways to get the characters out of the String. One way is to use the Character Array approach like Chuck used. Another is to use the String#charAt(index) method. You can get the API for String by looking it up from here: Java 6.0 API. Look in the bottom left side for a list of all the Core classes, click on String, and it will show you the methods available.

The problem with what you want to do is that you can not change str3 once it has been created. Strings are immutable, so once you do new String() the value can not be changed - you can not assign new characters into the sequence.

So you have to build the character sequence for str3 before you make the String. For example:


Looks like an awful lot to make a copy of a String. Luckily, you do not have to do it yourself. You can use:

Which does the same thing for you.

I know that s1 = s3 will do this job

Not exactly, what that does is make s1 and s3 point to the same String (or Character Sequence), not make s3 a new String with the copy of the characters in s1.

* Note on edits - It pays to write compilable code before posting - even for simple examples like above. Saves a lot of corrections on code and then replacing all the escaped characters... *


Steve
Manish R Singh
Greenhorn

Joined: Mar 14, 2009
Posts: 14
[MG] Removed "Be Nice" violation
As Mr chan specified that he wanted to access character by character not how to create string !


this modified and revised example for code provided by mr mondi said
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
Thanks a ton to all!

I tried the code using toCharArray(). That is, convert the string to character array and then to string. I succeeded in that.
Actually I forgot to mention it. But, i dont want that type of code. I want if any logic/method exist like this...

s3[2] = s1.charAt(2);//Ofcourse it is blunder mistake(just for refernce). I want to send character at index 2 of s1 to index 2 of s3 directly. Is there any solution?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Hagemaru Shou wrote:Steve wonder from wonderland you gotta learn alphabets first before forming Words .

Not sure what to make of this. I can't understand it... maybe it is a poor choice of Words?
Hagemaru Shou wrote:As Mr jackie chan specified that he wanted to access character by character not how to create string !

That would be 'Chan Lee' not 'Jackie Chan'. Is this an attempt at humor are you being rude? And, let's quote him:
how to access character by character from the String s1 and place one by one into s3?

He wants to copy, character by character from one String into the other, not just how to Access the characters. Which I showed how to do. I then showed that he doesn't have to do it because the new String(String) constructor does it for him.

Hagemaru Shou wrote:this modified and revised example for code provided by mr randi said

That would be Mr. Mondi who first posted the character array.


Chan Lee wrote:s3[2] = s1.charAt(2);//Ofcourse it is blunder mistake(just for refernce). I want to send character at index 2 of s1 to index 2 of s3 directly. Is there any solution?


No, unfortunately you can not do that. s3 is a String, and the characters in the String can not be assigned after the String is made. You must first build the character array first, then make a new String from the character array.

An alternative would be to use a StringBuilder object to do the work for you:
Manish R Singh
Greenhorn

Joined: Mar 14, 2009
Posts: 14
We are here to discuss java problem not lingual issues ,i guess i should suggst you some other site! Mr Luke worm


no need for character array gotta learn basics




i guess you were looking for this kind of code no character buffers just copy string from one string to another isng charAt
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Hagemaru Shou wrote:


i guess you were looking for this kind of code no character buffers just copy string from one string to another isng charAt


Although it looks like it uses no buffers, it actually is creating 2 new Strings for every character in the original String:
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Hagemaru Shou wrote:We are here to discuss java problem not lingual issues ...


Agreed. Let's continue to talk about the code and the solution.
Manish R Singh
Greenhorn

Joined: Mar 14, 2009
Posts: 14
So what luke its is using charAt() what mr chan wanted i think you have got lots of spare time i have got none stop this discussion lets chan decide what is best for him understood chillout man
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Steve Luke wrote:
An alternative would be to use a StringBuilder object to do the work for you:


I want to expound on the StringBuilder example. You should read the API for StringBuilder (see the link above) to learn how to use it. For example, you can use the StringBuilder#setCharAt() method to set the character at a particular position:

In this particular example, I like the StringBuilder#append() better, but if you want character-by-character access on both sides in a random-access manner, then the StringBuilder#setCharAt() method may be helpful.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

Hagemaru Shou wrote:So what luke its is using charAt() what mr chan wanted i think you have got lots of spare time i have got none stop this discussion lets chan decide what is best for him understood chillout man


Hi Hagemaru, I was not attacking your code. I was merely making an attempt at point out some thoughts about efficiency and scalability. Please don't take it as an attack on you or your code in any way, it is not meant that way, it is just a point of logistics.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
Hagemaru Shou, you have definitely breached the no 1 rule of JavaRanch: read this. Rudeness to others who are trying to help, in particular the tone of your replies to Steve Luke, is quite unacceptable; please apologise to him
Chan Lee
Greenhorn

Joined: Sep 02, 2008
Posts: 25
s2=s2.concat(String.valueOf(s1.charAt(d)));

If you dont mind!!! Can you explain the code in green color. That is, how it works?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Check out java.lang.String, and check what each separate call does:
- s1.charAt(d)
- String.valueOf(xxx)
- s2.concat(yyy)


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing of character by character from a String...