This question already has answers here:
How Do I Use VBScript to Strip the First n Characters of a String?
(4 answers)
Closed 6 years ago.
I have a program that prints a string to a notepad file, the output being something random:
#'f7ruhigbergbn
I want to however remove the first 3 characters from the pasted result, how can I do this?
myString = "#'f"
result = workings - myString
This does not work, bare in mind the first 3 characters are always going to be #'f
Any thoughts? thanks
You can use:
result = Mid(workings, 4)
You can use Right function for get the X characters of the right side of string. With Len function you can get the length of the string.
Right(myString,Len(myAtring) - 3)
With this, you get a new string whitout the three first characters, now you can assign to the same string:
myString = Right(myString,Len(myAtring) - 3)
Try this:
mystring = "#'f7ruhigbergbn"
result = Mid(mystring, 3, mystring.length)
Related
This question already has an answer here:
vbscript split string with colon delimiter
(1 answer)
Closed 1 year ago.
I am trying to come up with a method for extracting information from a file heading. The overall naming convention of the file heading will remain the same but portions of the heading will vary in character length. Below are two possible examples of such file headings:
012345678-012345-xxxx-yyyyy.txt
012345678-012345-xxx-yyyyyy.txt
Is there a way to extract values from these file headings such that it returns whatever appears between the second and third hyphen? Using the examples above it would return:
xxxx
xxx
Furthermore, is it possible to extract the values between the final hyphen and the period? Using the example above it would return:
yyyyy
yyyyyy
Extracting values is trivial when the character lengths are fixed, but I don't know if it's possible to do a similar extraction when the character lengths vary. I would normally use something like this to extract the information from a fixed-length naming convention but don't know how to adapt it to something where the character lengths change. For example, the snippet below is a function which extract the first nine characters in a file heading (in this case it would extract '012').
Function getthething(foo)
getthething = Mid(foo,1,3)
End Function
Any guidance would be very appreciated. Thank you.
You can do all of this using the Split function. Here's a wrapper function that simplifies things:
Function GetField(p_sText, p_sDelimiter, p_iIndex)
Dim arrFields
arrFields = Split(p_sText, p_sDelimiter)
If UBound(arrFields) >= (p_iIndex - 1) Then
GetField = arrFields(p_iIndex - 1)
Else
GetField = ""
End If
End Function
You can use this function like this:
Dim sFileName
Dim sYs
sFileName = GetField("012345678-012345-xxxx-yyyyy.txt", ".", 1)
sYs = GetField(sFileName, "-", 4)
MsgBox sYs
or simply:
MsgBox GetField(GetField("012345678-012345-xxxx-yyyyy.txt", ".", 1), "-", 4)
I am wondering how to make something where if X=5 and Y=2, then have it output something like
Hello 2 World 5.
In Java I would do
String a = "Hello " + Y + " World " + X;
System.out.println(a);
So how would I do that in TI-BASIC?
You have two issues to work out, concatenating strings and converting integers to a string representation.
String concatenation is very straightforward and utilizes the + operator. In your example:
"Hello " + "World"
Will yield the string "Hello World'.
Converting numbers to strings is not as easy in TI-BASIC, but a method for doing so compatible with the TI-83+/84+ series is available here. The following code and explanation are quoted from the linked page:
:"?
:For(X,1,1+log(N
:sub("0123456789",ipart(10fpart(N10^(-X)))+1,1)+Ans
:End
:sub(Ans,1,length(Ans)-1?Str1
With our number stored in N, we loop through each digit of N and store
the numeric character to our string that is at the matching position
in our substring. You access the individual digit in the number by
using iPart(10fPart(A/10^(X, and then locate where it is in the string
"0123456789". The reason you need to add 1 is so that it works with
the 0 digit.
In order to construct a string with all of the digits of the number, we first create a dummy string. This is what the "? is used
for. Each time through the For( loop, we concatenate the string from
before (which is still stored in the Ans variable) to the next numeric
character that is found in N. Using Ans allows us to not have to use
another string variable, since Ans can act like a string and it gets
updated accordingly, and Ans is also faster than a string variable.
By the time we are done with the For( loop, all of our numeric characters are put together in Ans. However, because we stored a dummy
character to the string initially, we now need to remove it, which we
do by getting the substring from the first character to the second to
last character of the string. Finally, we store the string to a more
permanent variable (in this case, Str1) for future use.
Once converted to a string, you can simply use the + operator to concatenate your string literals with the converted number strings.
You should also take a look at a similar Stack Overflow question which addresses a similar issue.
For this issue you can use the toString( function which was introduced in version 5.2.0. This function translates a number to a string which you can use to display numbers and strings together easily. It would end up like this:
Disp "Hello "+toString(Y)+" World "+toString(X)
If you know the length of "Hello" and "World," then you can simply use Output() because Disp creates a new line after every statement.
I want to find a specific character in a given string of number for example if my input is:
1 4 5 7 9 12
Then for 4 the answer should be 1. My code is as follows:
secarr = second.split(" ")
answer = secarr.index(number) #here number is a variable which gets the character
puts answer
The above method works if I write "4" instead of number or any other specific character but does not work if I write a variable. Is there a method in ruby to do the same?
This is probably your variable number is an Integer, and secarr is an Array of Strings. Try to cast the number to string:
answer = secarr.index(number.to_s)
This question already has answers here:
Converting an integer to a hexadecimal string in Ruby
(5 answers)
Closed 9 years ago.
I have code as below:
x = readsomevalue();
print x
The printed value is an integer. I want this in hex but,
The integer which is returned(x) is having leading 0 in it(ex. 0543676). for this the standard method fails
x.to_s(16) = 0
how to solve this?
since some of you have marked it as duplicate let me tell you, that the standard call fails when we have leading 0 in it, so is there anything to eliminate leading 0.
Fixnum#to_s
255.to_s(16) # => "ff"
Kernel#format:
'%x' % 255 # => "ff"
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert character code to what I want?
I am positive this has to be a duplicate, but all search results were in other languages or were the reverse (character to code point).
e.g.
charCode = 96
string = # ... ?
You could do it with:
string = charCode.chr
The .chr method on Fixnum.
96.chr #=> "c"