3 Replies - 4036 Views - Last Post: 19 June 2018 - 03:32 PM

#1 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 24
  • View blog
  • Posts: 555
  • Joined: 29-October 16

Easiest Way to Pull a Substring from a Managed String^

Posted 19 June 2018 - 07:56 AM

Hey all,
I know this sounds like a dumb question; but I've been searching for this for a minute here; and I've seen a lot of conflicting answers. You guys always set me straight; so I'm just going to ask:

what's the easiest, most efficient way to pull two characters from the front of a managed String^ ??? This is very easy using std::string; but with managed Strings I've found lots of different answers.

If you've got some extra time, it would be cool also to know what some other techniques are for pulling substrings from String^ s .. There's apparently ways you can pull specific character sequences; but I'm more concerned ATM with just pulling two characters from the beginning.

String^ sourceComputerSubstr = sourceComputer->Substring(0, 2);



^^/>^ that throws an System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string.
Parameter name: length' exception ... what am I doing wrong?

thanks in advance =]

This post has been edited by hexagod: 19 June 2018 - 07:56 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Easiest Way to Pull a Substring from a Managed String^

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2726
  • View blog
  • Posts: 4,365
  • Joined: 21-June 11

Re: Easiest Way to Pull a Substring from a Managed String^

Posted 19 June 2018 - 08:06 AM

That sounds like your string contains less then two characters.
Was This Post Helpful? 1
  • +
  • -

#3 hexagod   User is offline

  • 😂😂😂
  • member icon

Reputation: 24
  • View blog
  • Posts: 555
  • Joined: 29-October 16

Re: Easiest Way to Pull a Substring from a Managed String^

Posted 19 June 2018 - 08:24 AM

ty!
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6763
  • View blog
  • Posts: 23,063
  • Joined: 05-May 12

Re: Easiest Way to Pull a Substring from a Managed String^

Posted 19 June 2018 - 03:32 PM

You could get the minimum of the length of the string and 2, and use that to determine how many characters to pull from the front of the string.
String^ sourceComputerSubstr = sourceComputer->Substring(0, min(sourceComputer->Length, 2));


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1