-
Feb 25th, 2005, 01:58 AM
#1
Thread Starter
Hyperactive Member
Converting String to String Array and vice-versa [Java]
Coding Style: NetBeans IDE 4.0 Beta2 [Java]
Given a string [strFormat] I need to convert it (using predefined delimiters {}) into a string array. And then I must be able to go the other way (convert the string array back into a string with delimiters {})
So for example:
Given a string strFormat = "{Hello}{Bye}{Ouch}"
I need to put this in a string array [strFormatArray] so that strFormatArray[0] = "Hello", strFormatArray[1] = "Bye", strFormatArray[2] = "Ouch"
Then later I need to be able to take the string array [strFormatArray] and convert it back to the original string [strFormat] with the delimiters.
Any easy way to accomplish this? [built in string and conversion functions]?
Any ideas?
-
Feb 25th, 2005, 07:11 AM
#2
Re: Converting String to String Array and vice-versa [Java]
I guess you can use:
int indexOf("String")
to find out where the first "{" is, then use it again to find out where the first "}" is.
Then use:
String substring(iStart, iEnd)
to take out what is in the middle
Then you have to choose your self if you just want to remember where you ended and next time use:
int indexOf("String", iStartToRead)
to read the rest, or if you want to make a new string with the rest of the string and read it from there.
ØØ
-
Jul 29th, 2014, 06:36 AM
#3
Registered User
Re: Converting String to String Array and vice-versa [Java]
You can make use of regular expression when splitting the string. You String split method.
Example
String delimiters = "{|{}";
String strFormat = "{Hello}{Bye}{Ouch}"
// analyzing the string
String[] tokensVal = strFormat .split(delimiters);
-
Aug 6th, 2014, 07:23 PM
#4
Addicted Member
Re: Converting String to String Array and vice-versa [Java]
Or something like this.
Code:
String myString = "{Hello}{Bye}{Ouch}";
myString = myString.replace("{", "");
String[] myStringArray = myString.split("}");
System.out.println(myString);
System.out.println(myStringArray[0]);
for (int x = 0; x < myStringArray.length; x++) {
System.out.print("{" + myStringArray[x] + "}");
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|