The Artima Developer Community
Sponsored Link

Java Answers Forum
Create String Array from String [Java]

5 replies on 1 page. Most recent reply: 8 hours ago by ami g

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   
    Next Topic
Flat View: This topic has 5 replies on 1 page
Shaitan

Posts: 11
Nickname: shaitan00
Registered: Feb, 2005

Create String Array from String [Java] Posted: Feb 28, 2005 12:12 AM
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 10: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 6: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 8: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 2:55 PM
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();
}

ami g

Posts: 14
Nickname: amitg123
Registered: Apr, 2014

Re: Create String Array from String [java][/java] Posted: Apr 17, 2014 3:32 AM
Reply to this message Reply
if you want to check the exmaple of string plit check this -> http://tutorialswithexamples.com/string-split-java-example/

Flat View: This topic has 5 replies on 1 page
    Next Topic Topic: Problem in runnig java code


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us