Solved

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

Posted on 2004-03-30
14
247 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
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

856 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