[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-03-30
14
Medium Priority
?
254 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

656 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