Solved

How to fetch a character by character in a sequence from string?

Posted on 2004-03-30
14
249 Views
Last Modified: 2010-03-31
Hai,

I want to fetch a character by character in a sequence from string. And i put the values into array.
for exp:
    String x = "499 afsdhjadsh";
   
    array[0] = "4";
    array[1] = "9";
    array[2] = "9";
    array[3] = " ";
             :
             :
    array[13] = "h";

         Here i fetch a character by character in a sequense from string and fix that values into the array manually. So, is there any funtions/methods to fetching a string character by character in a sequence and fix that values into array.

for exp:
       byte[] bytes = x.getBytes();
               
I think u get my point. ok, fine pls. reply ASAP          

Regards,

Friendly Arul.
0
Comment
Question by:friendlyarul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +3
14 Comments
 

Author Comment

by:friendlyarul
ID: 10713590
--):
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 10713591
is this what you need?

char[] array = myString.toCharArray();

0
 
LVL 35

Expert Comment

by:TimYates
ID: 10713595
String str = "Woo woo" ;
char[] chars = new char[ str.length() ] ;
str.getChars( 0, str.length(), chars, 0 )  ;
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 35

Expert Comment

by:TimYates
ID: 10713600
Hahahaha, or what Nick_72 said ;)

Back to sleep for me ;)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10713639
If you want to turn a String into an array of String you can do the following:

String x = "499 afsdhjadsh";
String[] array = new String[x.length()];
for(int i = 0;i < array.length;i++) {
      array[0] = "" + x.charAt(i);  
}
0
 
LVL 7

Expert Comment

by:searlas
ID: 10713747
Same end result as CEHJ's answer, but using different method:
String split[] = "499 afsdhjadsh".split(""); // unfortunately split[0] is "" - not what we want.
String array[] = new String[split.length - 1];
System.arraycopy(split, 1, array, 0, array.length);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10713781
>>String split[] = "499 afsdhjadsh".split("");

would in fact be sufficient in itself
0
 
LVL 9

Expert Comment

by:Nick_72
ID: 10713825
well, as searlas says,

String split[] = "499 afsdhjadsh".split("");

will cause the first element to be an empty String.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10713873
Oh yes - so it is!
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 10714228
But this solves that problem:


            String x = "499 afsdhjadsh";
            String[] array = x.split("(?<!^)");
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10721280
Hmmm. Or maybe:

for ( int i = 0, iLen = x.length () ; i < iLen ; i ++ )
  array[i] = Character.toString ( x.charAt ( i ) ) ;
0
 
LVL 7

Expert Comment

by:searlas
ID: 10722203
Might as well add one more for the complete set:
for ( int i = 0; i < array.length; i++ ) {
  array[i] = x.substring(i, i+1);
}

That one has the property that all the String elements of array share the same internal char array.

(BTW CEHJ, kudos for reading the Pattern docs and working out that negative-assertion look behind thing.  I'd have thoughtit SHOULD break on splitting "two\nlines" but it doesn't, which is nice.)
0
 

Author Comment

by:friendlyarul
ID: 10722369
Hai,

"Good Answer"


0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10722408
8-)

>>BTW CEHJ, kudos for ...

LOL thanks
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question