Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

String Manipulation

string

  • Please log in to reply
4 replies to this topic

#1 Apprentice123

Apprentice123

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 457 posts

Posted 26 April 2012 - 05:21 AM

I have the following string:
["abc","bbbaa","cdgf","gtdds"  ....]

It's possible create a vector like this:
arr[0] = abc
arr[1] = bbbaa
arr[2] = cdgf
arr[3] = gtdds
.
.
.

?
  • 0

#2 sepp2k

sepp2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 183 posts
  • Programming Language:C, Java, C++, Python, Ruby, Scheme, Bash, Haskell, Others
  • Learning:Others

Posted 26 April 2012 - 05:33 AM

You can use String.substring to take the substring from the second character to the second to last character (i.e. everything but the brackets). Then you can use String.split to split the string by the commas. So you get an array containing the strings "\"abc\"", "\"bbbaa\"" etc. Then you can go through that array with a for-loop and use String.substring again to get rid of the quotes the same way you did with the brackets.
  • 1

#3 Apprentice123

Apprentice123

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 457 posts

Posted 26 April 2012 - 05:40 AM

I'm stating now in java. Do you hava a example?
  • 0

#4 sepp2k

sepp2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 183 posts
  • Programming Language:C, Java, C++, Python, Ruby, Scheme, Bash, Haskell, Others
  • Learning:Others

Posted 26 April 2012 - 06:19 AM

Here's an example of how to use substring to remove first and last characters from a string:

String foo = "[hello]";
foo = foo.substring(1, foo.length - 1);
// foo is now equal to "hello"

Here's an example of splitting a string:
String foo = "a,b,c";
String[] words = foo.split(",");
// words now contains the strings "a", "b" and "c"

  • 0

#5 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 397 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 26 April 2012 - 06:20 AM

Is this the String you are starting with: "["abc","bbbaa","cdgf","gtdds"....]"

Do you want an array with exactly 4 elements as a result of the parsing of the String?
I'm confused by your post that shows a column of 3 .s below the array elements.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download