We help IT Professionals succeed at work.

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

friendlyarul
friendlyarul asked
on
Medium Priority
270 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.
Comment
Watch Question

Author

Commented:
--):

Commented:
is this what you need?

char[] array = myString.toCharArray();

CERTIFIED EXPERT
Top Expert 2004

Commented:
String str = "Woo woo" ;
char[] chars = new char[ str.length() ] ;
str.getChars( 0, str.length(), chars, 0 )  ;
CERTIFIED EXPERT
Top Expert 2004

Commented:
Hahahaha, or what Nick_72 said ;)

Back to sleep for me ;)
CERTIFIED EXPERT
Top Expert 2016

Commented:
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);  
}

Commented:
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);
CERTIFIED EXPERT
Top Expert 2016

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

would in fact be sufficient in itself

Commented:
well, as searlas says,

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

will cause the first element to be an empty String.
CERTIFIED EXPERT
Top Expert 2016

Commented:
Oh yes - so it is!
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Hmmm. Or maybe:

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

Commented:
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.)

Author

Commented:
Hai,

"Good Answer"


CERTIFIED EXPERT
Top Expert 2016

Commented:
8-)

>>BTW CEHJ, kudos for ...

LOL thanks
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.