12 Replies - 581 Views - Last Post: 26 February 2016 - 12:19 PM Rate Topic: -----

#1 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Ways to split a string or store string value into array.

Posted 26 February 2016 - 09:47 AM

I need to take in a message that will look like this: "sGroup,Port"

I need to store "s", "Group" and "Port" as separate strings. I know I could split group and port because of the handy comma, but I am not sure if I could split the s, without using REGEX (which I've been told not to use).

So my other option is to store each string value as an array. But I would like to avoid using an array because the size of "Group" and "Port" may change.

Is there a way to split out the s?

Is This A Good Question/Topic? 0
  • +

Replies To: Ways to split a string or store string value into array.

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15260
  • View blog
  • Posts: 61,140
  • Joined: 12-June 08

Re: Ways to split a string or store string value into array.

Posted 26 February 2016 - 09:49 AM

Is 's' a single character or digit, or something else?

Do you have actual examples of the data?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7052
  • View blog
  • Posts: 23,972
  • Joined: 05-May 12

Re: Ways to split a string or store string value into array.

Posted 26 February 2016 - 09:53 AM

Out of curiosity, why the edict against regular expressions?
- speed
- memory
- fear of memory leaks
- fear of "disk leaks"
- maintenance: (few team members understand regular expressions)
- personal preference
Was This Post Helpful? 0
  • +
  • -

#4 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Ways to split a string or store string value into array.

Posted 26 February 2016 - 09:53 AM

View Postmodi123_1, on 26 February 2016 - 09:49 AM, said:

Is 's' a single character or digit, or something else?

Do you have actual examples of the data?

s needs to be a single character.

So the client sends this message:

"sGroup,Port"

I need to run an if Statement verifying that "s" was sent from the client. SO i need it separated out so I could do if (var check == "s")....DoStuff
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7052
  • View blog
  • Posts: 23,972
  • Joined: 05-May 12

Re: Ways to split a string or store string value into array.

Posted 26 February 2016 - 09:55 AM

So you don't even need a string for that part. Just get the first character: message[0] will give you the 's' if the string message contains "sGroup,Port".
Was This Post Helpful? 0
  • +
  • -

#6 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Ways to split a string or store string value into array.

Posted 26 February 2016 - 09:56 AM

View PostSkydiver, on 26 February 2016 - 09:53 AM, said:

Out of curiosity, why the edict against regular expressions?
- speed
- memory
- fear of memory leaks
- fear of "disk leaks"
- maintenance: (few team members understand regular expressions)
- personal preference

I would love to use REGEX I was just told it was not needed....
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7052
  • View blog
  • Posts: 23,972
  • Joined: 05-May 12

Re: Ways to split a string or store string value into array.

Posted 26 February 2016 - 10:01 AM

Yup, it is not. You can get by with the array access, IndexOf(), and Substring().
Was This Post Helpful? 1
  • +
  • -

#8 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15260
  • View blog
  • Posts: 61,140
  • Joined: 12-June 08

Re: Ways to split a string or store string value into array.

Posted 26 February 2016 - 10:02 AM

Okay.. so no example data. Gotcha.

Well - if it is one character then use it as an array's first character. Easy peasy.

-=--
derp.. should have refreshed. Skydiver's on it.

Was This Post Helpful? 0
  • +
  • -

#9 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Ways to split a string or store string value into array.

Posted 26 February 2016 - 10:15 AM

SubString is perfect! Makes it very simple... Still inexperienced so I don't always recall those easy to use methods.
Was This Post Helpful? 0
  • +
  • -

#10 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Ways to split a string or store string value into array.

Posted 26 February 2016 - 11:37 AM

So I am trying to assign the value "s" to vScubscribe, but below the code is what the value is when I run in debug mode. Does 0, not grab the s?


string vSubscribe = vClientMessage.Substring(0);


vSubscribe = "sDVRA1,DVT100"
Was This Post Helpful? 0
  • +
  • -

#11 jphoc13   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 584
  • Joined: 08-July 13

Re: Ways to split a string or store string value into array.

Posted 26 February 2016 - 11:50 AM

ok to grab first element it needs to be substring(0,1)
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,228
  • Joined: 12-December 12

Re: Ways to split a string or store string value into array.

Posted 26 February 2016 - 12:02 PM

For the first character I would just use vClientMessage[0] as was mentioned earlier.
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15260
  • View blog
  • Posts: 61,140
  • Joined: 12-June 08

Re: Ways to split a string or store string value into array.

Posted 26 February 2016 - 12:19 PM

There's always reading the docs to see what is happening with a new function.
https://msdn.microso...=vs.110%29.aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1