The Artima Developer Community
Sponsored Link

Java Answers Forum
Create String Array from String [Java]

4 replies on 1 page. Most recent reply: Mar 2, 2005 11:55 AM by Grant Swanjord

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Shaitan

Posts: 11
Nickname: shaitan00
Registered: Feb, 2005

Create String Array from String [Java] Posted: Feb 27, 2005 9:12 PM
Reply to this message Reply
Advertisement
-- Coding in NetBeans IDE 4.0 Beta2
 --
 
Given a string [sMessage] with variable amount of fields separated by a delimiter (“~”), I need to create a String Array [saMessage] so that I can manipulate the fields in an easier form. The string needs to be broken down, then certain sections are checked/updated and I can then re-create the string and send it off (through my Sockets).
But all I need is to figure out how to get the String into String[] format…
 
So for example:
sMessage = “Basic~Level 0~192.168.1.100~Index=1~RQ~
(or something like that, we can move/add/remove the “~” delimiter to make it easier to put into the array if required)
I need to convert the string sMessage into the following string[] array
saMessage[] = [“Basic”, “Level 0”, “192.168.1.100”, Index=1, RQ]
 
This makes it easier for me to manipulate each section of the string (when in the string array form), that way I can do stuff like saMessage[0] = Advanced, etc…..
Any ideas? (I tried playing with the String and String[] classes and found nothing that could help, do I need to import something?


Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: Create String Array from String [java][/java] Posted: Feb 28, 2005 7:34 AM
Reply to this message Reply
Hi,

If you are user jdk 1.4 or higher, then this should solve your problem.

String sMessage = "Basic~Level 0~192.168.1.100~Index=1~RQ~";
String saMessage[] = sMessage.split("~");


But remember that this will reduce the performance of your application, as the split() method uses regular expression.

Regards,
Amol Brid.

perry anderson

Posts: 10
Nickname: anderson
Registered: Feb, 2005

Re: Create String Array from String [java][/java] Posted: Feb 28, 2005 3:39 PM
Reply to this message Reply
nope, sorry, can't be done

tough

- perry

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Create String Array from String [java][/java] Posted: Mar 2, 2005 5:52 AM
Reply to this message Reply
There is one solution: Use a Object


You can't STORE an array in a simple String.


Object   s = "a~b~cd~ef";
Object[] s = ((String)s).spit("~");
 
//To access the array, use the following Syntax
String string2 = ((String[])s)[2];
 

Grant Swanjord

Posts: 2
Nickname: gswanjord
Registered: Jan, 2003

Re: Create String Array from String [java][/java] Posted: Mar 2, 2005 11:55 AM
Reply to this message Reply
I've always been partial to tokenizers....

StringTokenizer st = new StringTokenizer(sMessage, "~");
int tokenCount = st.countTokens();
String[] array = new String[tokenCount];
 
for (int i = 0; i < tokenCount; i++) {
    array[i] = st.nextToken();
}

Flat View: This topic has 4 replies on 1 page
Topic: WSAD migration question Previous Topic   Next Topic Topic: Drawing Graphs with GUI

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use