How to capitalize first letter of a string in VBScript - vbscript

I read a really helpful post here How do I make the first letter of a string uppercase in JavaScript? and I was curious of a good solution using VBScript.

>> s = "first word of string"
>> WScript.Echo UCase(Left(s, 1)) & Mid(s, 2)
>>
First word of string

str = "mY nAME iS sACHIN"
arrStr = split(str," ")
For i=0 to ubound(arrStr)
word = lcase(trim(arrStr(i)))
word = replace(word,mid(word,1,1),chr(asc(mid(word,1,1))-32),1,1)
str1 = str1 & word & " "
next
msgbox trim(str1)
Output: My Name Is Sachin

str = "mY nAME iS sACHIN"
arrStr = split(str," ")
For i=0 to ubound(arrStr)
word = lcase(trim(arrStr(i)))
word = replace(word,mid(word,1,1),ucase(mid(word,1,1)),1,1)
str1 = str1 & word & " "
next
msgbox trim(str1)

Related

How to remove quotes from strings in an array?

Assuming I have the following input:
names = ["\"Петр Сергеевич\"", "\"Курсатов Роман\"", "\" \"", "\"Павел2 Олегович\"", "\"Илья иванович\"", "\" \""]
Each whitespace is actually a non-breaking space (U+00A0).
How do I remove \" in pure ruby, so the following is true:
p names
=> ["Петр Сергеевич", "Курсатов Роман", " ", "Павел2 Олегович", "Илья иванович", " "]
I tried:
names.map { |i| i.gsub(/[\"]/, "")}.map(&:inspect)
names.map { |i| i.delete('\\"')}.map(&:inspect)
names.map { |i| i.gsub('\\"', '')}.map(&:inspect)
Nothing seems to work.
string.delete("\"")
# => " "
or
string.tr("\"", "")
# => " "

visual basic simple calculator

I wrote simple calculator with visual basic but when I debug my code, it has problem and dose not run correctly .
my code
Public Sub general()
Dim num1 As Long, num2 As Long
Dim result As Single
Dim op As String
End Sub
Private Sub Form_Load()
Text1 = " "
num1 = "0"
num2 = "0"
op = " "
one.Caption = "1"
two.Caption = "2"
three.Caption = "3"
four.Caption = "4"
five.Caption = "5"
six.Caption = "6"
seven.Caption = "7"
eight.Caption = "8"
nine.Caption = "9"
zero.Caption = "0"
clear11.Caption = "cls"
Equal12.Caption = "="
plus.Caption = "+"
min14.Caption = "-"
multi15.Caption = "*"
div16.Caption = "/"
lnx17.Caption = "1/x"
power18.Caption = "x^2"
arc19.Caption = "aqr(x)"
exit20.Caption = "Exit"
End Sub
Private Sub one_Click()
Text1 = Text1 + "1"
End Sub
Private Sub two_Click()
Text1 = Text1 + "2"
End Sub
Private Sub three_Click()
Text1 = Text1 + "3"
End Sub
Private Sub four_Click()
Text1 = Text1 + "4"
End Sub
Private Sub five_Click()
Text1 = Text1 + "5"
End Sub
Private Sub six_Click()
Text1 = Text1 + "6"
End Sub
Private Sub seven_Click()
Text1 = Text1 + "7"
End Sub
Private Sub eight_Click()
Text1 = Text1 + "8"
End Sub
Private Sub nine_Click()
Text1 = Text1 + "9"
End Sub
Private Sub zero_Click()
Text1 = Text1 + "0"
End Sub
Private Sub clear11_click() 'cls button
test1 = ""
num1 = "0"
num2 = "0"
op = ""
End Sub
Private Sub Equal12_click() 'Equal button
num2 = Val(Text1)
Select Case op
Case "+"
result = num1 + num2
Case "-"
result = num1 - num2
Case "*"
result = num1 * num2
Case "/"
result = num1 / num2
Case "1/x"
result = 1 / num1
Case "x^2"
result = num1 ^ 2
'Case "aqr(x)"
'result = aqr(num1)
End Select
Text1 = Str(result)
op = ""
End Sub
Private Sub plus_click()
num1 = Val(Text1)
op = "+"
Text1 = ""
End Sub
Private Sub min14_click()
num1 = Val(Text1)
op = "-"
Text1 = ""
End Sub
Private Sub multi15_click()
num1 = Val(Text1)
op = "*"
Text1 = ""
End Sub
Private Sub div16_click()
num1 = Val(Text1)
op = "/"
Text1 = ""
End Sub
Private Sub squareroot19_click() 'sqr(x)button
num1 = Val(Text)
result = Sqr(num1)
Text1 -Str(result)
End Sub
when I debug it I find the problem is "op" variable that not set in correct way because when I click on equal button and the program go to select case part the value of "op" is empty.
can any one help me how to fix it?
I've run the following in a single function and it seems to work for me. I get 3 when running the code within the methods to click 1, click +, and click 2.
I basically put the output of this into a textbox on a windows form, which displayed 3 when I ran this code.
I did add the line Dim text1 As String, did you declare this elsewhere?
Dim num1 As Long, num2 As Long
Dim result As Single
Dim op As String
Dim text1 As String
text1 = " "
num1 = "0"
num2 = "0"
op = " "
text1 = text1 + "1"
num1 = Val(text1)
op = "+"
text1 = ""
text1 = text1 + "2"
num2 = Val(text1)
Select Case op
Case "+"
result = num1 + num2
Case "-"
result = num1 - num2
Case "*"
result = num1 * num2
Case "/"
result = num1 / num2
Case "1/x"
result = 1 / num1
Case "x^2"
result = num1 ^ 2
'Case "aqr(x)"
'result = aqr(num1)
End Select
text1 = Str(result)
op = ""
Declare your varibles outside the general method. This will make the methods available to get/set them. An alternative could be to pass the buttons value as a parameter, but the first part is the easier!

Ruby: Is it possible to set an if statement == to 2 variables?

I am making a simple program that censors a users input based on the words chosen.
puts "Enter your Text: "
text = gets.chomp.downcase!
puts "Word to censor: "
censor1 = gets.chomp
censor1.downcase!
puts "Second word to censor: "
censor2 = gets.chomp
censor2.downcase!
words = text.split(" ")
words.each do |letter|
if letter == censor1
print "CENSORED "
else
print words + " "
end
end
So, is it possible to set: 'if letter == censor1 and censor 2' ?
You can check if object present in array with the help of Array#include?
if [censor1, censor2].include?(letter)
A bit simplified variant of your code:
puts 'Enter your Text: '
text = gets.chomp.downcase
puts 'Word to censor: '
censor1 = gets.chomp.downcase
puts 'Second word to censor: '
censor2 = gets.chomp.downcase
text.split(' ').each do |word|
if [censor1, censor2].include?(word)
print 'CENSORED '
else
print word + ' '
end
end
And much easier and better solution with String#gsub which replaces all occurrences of censored words in text:
puts 'Enter your Text: '
text = gets.chomp.downcase
puts 'Word to censor: '
censor1 = gets.chomp.downcase
puts 'Second word to censor: '
censor2 = gets.chomp.downcase
[censor1, censor2].each { |c| text.gsub!(c, 'CENSORED') }
puts text
If you are asking if either (downcased) censorX contains the character letter:
censor1+censor2.include?(letter)
If you are asking if both censorX's contain the character letter:
censor1.include?(letter) && censor1.include?(letter)
If you are asking if all censorX strings equal the string letter, then:
if [censor1, censor2,...].uniq == [letter]
...
end

Check if array contains string, case insensitive

I'm trying to write an application that removes words from a list of words:
puts "Words:"
text = gets.chomp
puts "Words to remove:"
remove = gets.chomp
words = text.split(" ")
removes = remove.split(" ")
words.each do |x|
if removes.include.upcase? x.upcase
print "REMOVED "
else
print x, " "
end
end
How would I make this case insensitive?
I tried putting .upcase in there but no luck.
words.each do |x|
if removes.select{|i| i.downcase == x.downcase} != []
print "REMOVED "
else
print x, " "
end
end
array#select will select any element from the array if the block yields true. So if select do not select any element and return an empty array, it is not in the array.
Edit
You can also use if removes.index{|i| i.downcase==x.downcase}. It performs better than select since it does not create a temporary array and returns whenever it finds the first match.
puts "Words:"
text = gets.chomp
puts "Words to remove:"
remove = gets.chomp
words = text.split(" ")
removes = remove.upcase.split(" ")
words.each do |x|
if removes.include? x.upcase
print "REMOVED "
else
print x, " "
end
end

Ruby String concatenation

I have an array
books = ["Title 1", "Title 2", "Title 3"]
I need to iterate through this array and get a variable like this:
#books_read = "Title 1 \n Title 2 \n Title 3"
I tried this bit of code:
books.each do |book|
#books_read += "#{book} \n"
end
puts #books_read
But, the + operator does not concatenate the strings. Any leads on this please.
Cheers!
You can use Array#join: books.join(" \n ").
join(sep=$,) → str
Returns a string created by converting each element of the array to a
string, separated by sep.
You can use join: books.join(" \n ")

Resources