[UT] inStr(), Mid(), Left(), Right() question...

  • Two Factor Authentication is now available on BeyondUnreal Forums. To configure it, visit your Profile and look for the "Two Step Verification" option on the left side. We can send codes via email (may be slower) or you can set up any TOTP Authenticator app on your phone (Authy, Google Authenticator, etc) to deliver codes. It is highly recommended that you configure this to keep your account safe.

Qjahe

New Member
Nov 15, 2003
36
0
0
Hi,

i've been trying to understand those above little string functions.

InStr Wiki description:
int InStr (coerce string S, coerce string t) [static]
If the string t is found inside S InStr returns the starting position with the first character of S being 0. If t is not found InStr returns -1.

I have the following string:
myString = "CONNECT MYIP LOGIN PWD PORT"

now InStr returns the POSITION of the first character found in the string. in this example it would be returning 7 if I had:
Code:
 i = InStr(myString," ");
I have the loc of my first space.. I can get CONNECT alone by using:
Code:
tmpString = Left(myString, i)
my Question is how do I get the rest seperately? how can I make inStr move to the next character(space) ?

anyone able to explain this to me?

Thanks :)

Qjahe
 

Silver5123

New Member
Sep 14, 2003
12
0
0
I presume you want to cut out each section of the string to different strings/variables? So...

Well, like you have:


Code:
myString = "CONNECT MYIP LOGIN PWD PORT";

i = InStr(myString, " ") 
//i = 7;

tmpString = Left(myString, i); 
//tmpString is the leftmost 7 characters "CONNECT"

myString = Right(myString, Len(myString) - i - 1)
//myString is now equal to the 19 right most characters (27 - 7 - 1) "MYIP LOGIN PWD PORT"

i = InStr(myString, " ");  
//i = 4

myIPString = Left(myString, i); 
//myIPString is the 4 left most characters "MYIP"

Then just keep doing the same thing for LOGIN PWD and PORT.
 

Qjahe

New Member
Nov 15, 2003
36
0
0
BreakEmUp()

I came up with this little function to Break a String up...
Code:
function BreakEmUp(string Text, string C){
  local int i, j;
  local string BEU[20];
  j=0;
  while(i != -1){
    i = inStr(Text,C);
    if(i != -1){
      BEU[j] = Left(Text,i);
      Text = Mid(Text,i+1)
      j++;
    }
  }
  return BEU;
}

function someOtherFunction(){
  local string B[20];
  B = BreakEmUp(Text," ");
}

When I try to compile it, it gives me the following error
Error, Bad or missing expression in '='

At the exact line where I am calling my function BreakEmUp... I can't return a whole Array in UScript? What am I doing wrong?

Thanks guys.

Qjahe
 

Wormbo

Administrator
Staff member
Jun 4, 2001
5,913
36
48
Germany
www.koehler-homepage.de
Silver5123 said:
Code:
myString = Right(myString, Len(myString) - i - 1)
This could be better done with
Code:
myString = Mid(myString, i+1);
Same effect, mut much less code and it's easier to understand what it does. (take all characters, beginning with the i+1st characters)

Qjahe: Your function needs a return value. (See UnrealWiki: Function Syntax)
 
Last edited:

Silver5123

New Member
Sep 14, 2003
12
0
0
Hehe...You didn't declare BreakEmUp to return anything.

Try:

function String[] BreakEmUp(string Text, string C)
 

Wormbo

Administrator
Staff member
Jun 4, 2001
5,913
36
48
Germany
www.koehler-homepage.de
Static arrays can't be used as the return value of a function. In UT2003 I'd have recommended using a dynamic array, but in UT you're screwed.

Maybe you should use an out parameter like this:
Code:
function BreakEmUp(string Text, string C, out string Parts[20]) {
...
}
 

Qjahe

New Member
Nov 15, 2003
36
0
0
Error, Declaration: Missing string size

when I declare it like this:
Code:
function String[] BreakEmUp(string Text, string C){
I get the error "Error, Declaration: Missing string size"...
when I decalre it like this:
Code:
function String[20] BreakEmUp(string Text, string C){
I get the warning:
"Warning, String sizes are now obsolete; all strings are dynamically sized"
at the Function declaration line...

and the following as error:
"Error, Array mismatch in '=' "
at the line where I call the function...

any ideas...

Thanks guys :)
 

Silver5123

New Member
Sep 14, 2003
12
0
0
Sorry, I was thinking of Java.


Wormbo has it right, you have to have an out string in the parameters.
 

Qjahe

New Member
Nov 15, 2003
36
0
0
yeah sorry I saw the answer after the post.

I'm still getting errors.
with the out parameter in the declaration how am I supposed to call it?
Code:
function someOtherFunction(){
  local string B[20];
  B = BreakEmUp(Text," ",B);
}

Thanks agian guys

Qj
 

Qjahe

New Member
Nov 15, 2003
36
0
0
Works

Alright it works now.
if anyone wants to use it here is what I have:
Code:
function BreakEmUp(string Text, string C, out string BEU[20]) {
  local int i, j;
  j=0;
  while(i != -1){
    i = inStr(Text,C);
    if(i != -1){
      BEU[j] = Left(Text,i);
      Text = Mid(Text,i+1)
      j++;
    }
    else{
      BEU[j] = Text;
    }
  }
  return;
}

function someOtherFunction(){
  local string B[20];
  BreakEmUp(Text," ",B);
}

the array of Strings B will now contain all the words in the string splitted at the Space character. Hope it helps anyone that was searching for a split Function in [UT] :)

Thanks again guys :D
Qj
 
Last edited: