Solved

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

Posted on 2004-03-30
14
246 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do i compare an object based on two fields 6 53
varialbe initialization 11 32
web application structure 18 76
jsp login check 12 29
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

785 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