Solved

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

Posted on 2004-03-30
14
244 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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:mayankeagle
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoresSpecial  challenge 13 40
return in catch statement 1 40
java set up 1 46
Eclipse Neon and jdk 1.8.0 11 69
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now