Why does this string return this way - jython

Why does this code return jython string and not pjthon string?
str = "python string"
new_str = "j" + str[1:]
print(new_str)
result is jython string

This code is just Python, not Jython specific.
It is using String slicing, which syntax looks like: str[ start_pos:end_pos:step ]
str[1:] is a slice of "python string", starting at index 1 with no end position or step size, will result in "ython string". (in Python, the first element is at index 0)
new_str = "j" + str[1:] will result in new_str = "j" + "ython string", which will set new_str to "jython string".
Try it online

Related

VB.NET - Delete excess white spaces between words in a sentence

I'm a programing student, so I've started with vb.net as my first language and I need some help.
I need to know how I delete excess white spaces between words in a sentence, only using these string functions: Trim, instr, char, mid, val and len.
I made a part of the code but it doesn't work, Thanks.
enter image description here
Knocked up a quick routine for you.
Public Function RemoveMyExcessSpaces(str As String) As String
Dim r As String = ""
If str IsNot Nothing AndAlso Len(str) > 0 Then
Dim spacefound As Boolean = False
For i As Integer = 1 To Len(str)
If Mid(str, i, 1) = " " Then
If Not spacefound Then
spacefound = True
End If
Else
If spacefound Then
spacefound = False
r += " "
End If
r += Mid(str, i, 1)
End If
Next
End If
Return r
End Function
I think it meets your criteria.
Hope that helps.
Unless using those VB6 methods is a requirement, here's a one-line solution:
TextBox2.Text = String.Join(" ", TextBox1.Text.Split(New Char() {" "c}, StringSplitOptions.RemoveEmptyEntries))
Online test: http://ideone.com/gBbi55
String.Split() splits a string on a specific character or substring (in this case a space) and creates an array of the string parts in-between. I.e: "Hello There" -> {"Hello", "There"}
StringSplitOptions.RemoveEmptyEntries removes any empty strings from the resulting split array. Double spaces will create empty strings when split, thus you'll get rid of them using this option.
String.Join() will create a string from an array and separate each array entry with the specified string (in this case a single space).
There is a very simple answer to this question, there is a string method that allows you to remove those "White Spaces" within a string.
Dim text_with_white_spaces as string = "Hey There!"
Dim text_without_white_spaces as string = text_with_white_spaces.Replace(" ", "")
'text_without_white_spaces should be equal to "HeyThere!"
Hope it helped!

VBA code lower case to upper case using Mid string

I've gotten stuck with this VBA code. Any help would be greatly appreciated. I'm trying to change the first letters of 2 words from lower case to upper case. Also, how should I take the space in between these two words into consideration in the code?
I haven't been able to execute the code as I keep getting this compile error: "Argument not optional".
Function Properword(Text)
Dim rText
rText = Len(rText)
If rText(Mid(1, 1)) = LCase(Str) Then
rText = UCase(Str)
If rText(Mid(6, 1)) = LCase(Str) Then
rText = UCase
End If
End Function
Cheers!
First of all, you don't have to use UDF. Simply use inbuilt WorksheetFunction.Proper function to achieve ProperCase.
If you still want to create UDF, an example would be
Function Properword(strTxt)
Dim arrTxt
arrTxt = Split(strTxt, " ")
For i = LBound(arrTxt) To UBound(arrTxt)
arrTxt(i) = UCase(Left(arrTxt(i), 1)) & Mid(arrTxt(i), 2)
Next
Properword = Join(arrTxt, " ")
End Function
Finally, issues with your code
rText = Len(rText) ~~ this means rText will contain a numeric value because Len returns the lenght of the string
If rText(Mid(1, 1)) = LCase(Str) Then ~~ Mid takes the string as first argument followed by start point and then end point (optional).
not sure what you were trying to do in the following lines.
rText = UCase(Str)
If rText(Mid(6, 1)) = LCase(Str) Then
rText = UCase
In addition to the Excel function PROPER
str = WorksheetFunction.Proper("UPPER lower") ' "Upper Lower"
There is also the VBA.StrConv function:
str = StrConv("UPPER lower", vbProperCase) ' "Upper Lower"
To convert only parts of the string to uppercase, you can use RegEx, or the Mid statement:
Mid(str, 1, 1) = UCase(Mid(str, 1, 1)) ' makes the first letter uppercase

Evaluate() not working inExcel VBA as expected

I have a user-defined function in an Excel VBA module. Let's say it's:
Function MyFunc( val1 as Double, val2 as int, val3 as int ) as Double
MyFunc = val1 * val2 + val3
End Function
I also have another Function MyEval:
Function MyEval( formula as string )
MyEval = Evaluate( formula )
End Function
In a worksheet, the cell B1 contains the following string:
"blah blah blah "&MyFunc(VAL,2,0)&" foobar "&MyFunc(VAL,6,5)&" do re mi"
Note that the quotes are part of the string, not just denoting that it is a string. In second cell, I have the following:
=MyEval(SUBSTITUTE(B1,"VAL",8))
In general this works. In the above case I would get, as expected, the string:
blah blah blah 16 foobar 53 do re mi
In some cases, however, I would get #VALUE!. When I examined those examples that failed, I would find that removing different parts would allow the string to evaluate, but that no specific part caused it to fail, i.e., just the first half works, just the second half works, and just the middle part works.
Thinking that it might be a length issue (strings are up to 200 characters), I wrote a new evaluation function, where I break the string into parts on the concatenate symbol (&), evaluate each part separately, and then concatenate the results together:
Function MyEval2(formula As String)
If Len(formula) = 0 Then
Eval2 = ""
Else
Dim parts() As String
parts = Split(formula, "&")
Dim result As String
result = ""
Dim index As Integer
Dim part As String
For index = LBound(parts) To UBound(parts)
part = parts(index)
If (Left(part, 1) = """") And (Right(part, 1) = """") Then
part = Left(part, Len(part) - 1)
part = Right(part, Len(part) - 1)
result = result & part
Else
Dim val As Variant
val = Evaluate(part)
result = result & val
End If
Next index
Eval2 = result
End If
End Function
In this case, sometimes when the variable 'part' was, for example, the string
MyFunc(8,6,5)
the line
val = Evaluate(part)
resulted in 'val' being the string
MyFunc(8,6,5)
instead of the value 53.
Note: I have extensively re-written this to make it clearer.

Inserting a word within a given string

I want the user to input a phrase such as "Python" and have the program put the word "test" in the middle.... So it would print "pyttesthon".
After I input the phrase however, I am not sure which function to use.
You can just concatenate strings like so:
stringToInsert = "test"
oldString = "Python"
newString = oldString[0:len(oldString)/2] + stringToInsert + oldString[len(oldString)/2:]

VB.NET Looping a String to a set Number

Suppose I need to add the ASCII version of each character in the word "hello" to "hi" so that the result would be something like this: (h+h = )(e+i = )(l+h = )(l+i = )(o+h = ) etc how would I go about looping the "hi" string?
I have already managed to loop the "hello" string, but not quite sure how to do the second without getting (h+h = )(h+i = )(e+h = )(e+i = ) etc.
Thanks!
You can use the Mod opreator to make the index start over. Example:
Dim str1 as String = "hello"
Dim str2 as String = "hi"
' This gets the length of the longest string
Dim longest = Math.Max(str1.Length, str2.Length)
' This loops though all characters
' The Mod operator makes the index wrap over for the shorter string
For i As Integer = 0 To longest - 1
Console.Write(str1(i Mod str1.Length))
Console.WriteLine(str2(i Mod str2.Length))
Next
Output:
hh
ei
lh
li
oh