How do you store a certain part of a string into a variable? - game-maker

How do you store a certain part of a string into a variable?
For example:
x = myString // - But store the 9th character into a variable

Try this,
x = string_char_at(myString , 9);

This gets a single character from a string:
var x = string_char_at("This is my string", 4); //X == "s"
And you can use the string_copy function to copy parts of a string;
var x = string_copy("This is my string", 8, 2); //X == "my"

Related

how to make double from string

My user never value to a pop up and I add .0 at the end however once I convert the value to double the value is edited to only 2 and I need the value 2.0
if (pResult.Ok && !string.IsNullOrWhiteSpace(pResult.Text))
{
var test = pResult.Text + ".0";
double doub = Convert.ToDouble(test);
_settingsService.TimeOutIdentification = doub;
}
Please use following code to convert it
string str1 = String.Format("{0:F}", test);

converting string to vector in Unity

I am trying to convert an array of strings in unityscript with values holding values like:
"Vector3(5, 3, 8)"
into an array of vectors, but Unity will not take these strings as is. Anyone have any ideas?
you cant convert all vector elements together in c# you can do it like bellow:
its psuedoCode:
position.x=convert.ToFloat("3");
position.y=....
i think there is no api to make this for you:
"Vector3(5, 3, 8)"
You didn't give us any sample code, so I'll assume the array is nicely organized, all elements right next to each other.
While I don't think there's any way that you can do this fully using UnityScript, there's ways you can process the literal string with other languages, that is, copying from your Unity script and into another program that will do the change for you.
Here's a small sample of what I would do.
Usage: Create a file array.txt with all the Vector3(x,x,x) strings, separated by line breaks and create another file array.php
Array.txt (sample)
Vector3(5, 1, 3)
Vector3(3, 3, 1)
Vector3(2, 2, 7)
Vector3(6, 6, 4)
Vector3(8, 8, 8)
Vector3(9, 3, 2)
Vector3(1, 2, 1)
Vector3(4, 3, 6)
Vector3(5, 3, 8)
Array.php
<?
$file = file('array.txt');
$text = array();
$text[] = "var vectors = new Array;";
foreach ( $file as $i )
{
$text[] = "vectors.Push(".trim($i).");";
}
echo implode("<br>", $text);
Then just run it under a PHP sandbox or a web server and copy and paste the new array into your script.
use this method to convert a single String value into a vector3 value
public static Vector3 StringToVector3(string sVector)
{
// Remove the parentheses
if (sVector.StartsWith ("(") && sVector.EndsWith (")")) {
sVector = sVector.Substring(1, sVector.Length-2);
}
// split the items
string[] sArray = sVector.Split(',');
// store as a Vector3
Vector3 result = new Vector3(
float.Parse(sArray[0]),
float.Parse(sArray[1]),
float.Parse(sArray[2]));
return result;
}
I figure someone else might find this useful later so:
if(PlayerPrefs.GetFloat("nodeNum") > 0) {
//Assemble axis value arrays
//X
var xString = PlayerPrefs.GetString("xVals");
var xValues = xString.Split(","[0]);
//Y
var yString = PlayerPrefs.GetString("yVals");
var yValues = yString.Split(","[0]);
//Z
var zString = PlayerPrefs.GetString("zVals");
var zValues = zString.Split(","[0]);
var countNode = 0;
var goal = PlayerPrefs.GetFloat("nodeNum");
var nodeVecs = new Array(Vector3.zero);
while (countNode != goal) {
var curVec = Vector3(float.Parse(xValues[countNode]), float.Parse(yValues[countNode]), float.Parse(zValues[countNode]));
nodeVecs.Push(curVec);
countNode += 1;
}
var convNodeVecs : Vector3[] = nodeVecs.ToBuiltin(Vector3) as Vector3[];
for(var nodeVec : Vector3 in convNodeVecs) {
Instantiate(nodeObj, nodeVec, Quaternion.identity);
}
}

asp.net Substring from specific string to the end of the word

I have a multiline textbox that user may type whatever he wants to for example,
"Hello my name is #Konstantinos and i am 20 #years old"
Now i want to place a button when is pressed the output will be #Konstantinos and #years -
Is that something that can be done using substring or any other idea?
Thank you in advance
If all that you want is HashTags(#) from the entire string, you can perform simple .Split() and Linq. Try this:
C#
string a = "Hello my name is #Konstantinos and i am 20 #years old";
var data = a.Split(' ').Where(s => s.StartsWith("#")).ToList();
VB
Dim a As String = "Hello my name is #Konstantinos and i am 20 #years old"
Dim data = a.Split(" ").Where(Function(s) s.StartsWith("#")).ToList()
Using regex will give you more flexibility.
You can define a pattern to search for strings starting with #.
.Net regex cheat sheet
Dim searchPattern = "#(\S+)" '\S - Matches any nonwhite space character
Dim searchString = "Hello my name is #Konstantinos and i am 20 #years old"
For Each match As Match In Regex.Matches(searchString, searchPattern, RegexOptions.Compiled)
Console.WriteLine(match.Value)
Next
Console.Read()
This will work . Try this..
string str = "Hello my name is #Konstantinos and i am 20 #years old asldkfjklsd #kumod";
int i=0;
int k = 0;
while ((i = str.IndexOf('#', i)) != -1)
{
string strOutput = str.Substring(i);
k = strOutput.IndexOf(' ');
if (k != -1)
{
Console.WriteLine(strOutput.Substring(0, k));
}
else
{
Console.WriteLine(strOutput);
}
i++;
}

How to increment a value in a map with Apex?

Is there a way to increment a value in a map without needing a second variable?
for example, this doesn't work:
counts = new Map<string,integer>();
counts.put('month_total',0);
counts.put('month_total',counts.get['month_total']++);
it returns "Initial term of field expression must be a concrete SObject: MAP"
instead I needed to do:
counts = new Map<string,integer>();
counts.put('month_total',0);
integer temp = 0;
temp++;
counts.put('month_total',temp);
is there any way to increment without needing an extra variable?
Replace
counts.put('month_total',counts.get['month_total']++);
with
counts.put('month_total',counts.get('month_total')++);
List<String> lststrings = new List<String>{'key','wewe','sdsd','key','dfdfd','wewe'};
Map<String,Integer> mapval = new Map<String,Integer>();
Integer count = 1;
for(string str : lststrings){
IF(mapval.containsKey(str)){
mapval.put(str,mapval.get(str)+1);
}
else{
mapval.put(str,count);
}
}
system.debug('value of mapval'+mapval);

ASP.NET looking for a way to pad a string with 0 or blanks

Hey im looking for a way to do the following to populate a text file
if I need to fill a alphanumeric column with Field size 20 and I only have 18 characters to append two blank values.
then same for numeric values if field size is 10 for example and i have a value of 5 characters to fill in remaining spaces with 5 0's
i.e instead of 10000 i would have 0000010000
string s = "10000";
string t = s.PadLeft(20, '0');
The PadLeft method should do the trick. Something like this:
var output = myTextString.PadLeft(20);
or
var output = myNumericString.PadLeft(10, '0');
Here's some pseudocode that should do it:
int size = mystring.length();
int padding = 20 - size;
string pad = "";
for(padding){
pad += "0";
}
string newstring = pad + mystring;
http://jsfiddle.net/qtzTu/
How to Pad a Number with Leading Zeroes:
http://msdn.microsoft.com/en-us/library/dd260048.aspx
The OP implied the string lengths would vary, so here is a Func where you won't have to hard code values like "10" or "20"
Func<string, int, string> PadStringToSize = (x,y)
=> (x.Length < y ? x.PadLeft(y, '0') : x);
You can then do things like:
Console.WriteLine(PadStringToSize("10000", 10)); // Pads to 10
Console.WriteLine(PadStringToSize("10000", 30)); // Pads to 30
Then you can just wrap the call to a method that takes the size as a parameter instead hard coding the desired length.

Resources