Kotlin: strip first and last character - kotlin

In Kotlin, I need to strip the first and last characters from a string. This seems to be getting compile errors:
val MyPiece = str.substring(0, str.length - 1)
What's wrong here?

You can also do:
val str = "hello"
val myPiece = str.drop(1).dropLast(1)
println(myPiece)

You can try this one:
val str = "myText"
var myPiece = str.substring(1, str.length -1)
print(myPiece)

Related

Longest Common substring breaking issue

Hi I have a function that finds the longest common substring between two strings. It works great except it seems to break when it reaches any single quote mark: '
This causes it to not truly find the longest substring sometimes.
Could anyone help me adjust this function so it includes single quotes in the substring? I know it needs to be escaped someplace I'm just not sure where.
Example:
String 1: Hi there this is jeff's dog.
String 2: Hi there this is jeff's dog.
After running the function the longest common substring would be:
Hi there this is jeff
Edit: seems to also happen with "-" as well.
It will not count anything after the single quote as part of the substring.
Here's is the function:
Public Shared Function LongestCommonSubstring(str1 As String, str2 As String, ByRef subStr As String)
Try
subStr = String.Empty
If String.IsNullOrEmpty(str1) OrElse String.IsNullOrEmpty(str2) Then
Return 0
End If
Dim num As Integer(,) = New Integer(str1.Length - 1, str2.Length - 1) {}
Dim maxlen As Integer = 0
Dim lastSubsBegin As Integer = 0
Dim subStrBuilder As New StringBuilder()
For i As Integer = 0 To str1.Length - 1
For j As Integer = 0 To str2.Length - 1
If str1(i) <> str2(j) Then
num(i, j) = 0
Else
If (i = 0) OrElse (j = 0) Then
num(i, j) = 1
Else
num(i, j) = 1 + num(i - 1, j - 1)
End If
If num(i, j) > maxlen Then
maxlen = num(i, j)
Dim thisSubsBegin As Integer = i - num(i, j) + 1
If lastSubsBegin = thisSubsBegin Then
subStrBuilder.Append(str1(i))
Else
lastSubsBegin = thisSubsBegin
subStrBuilder.Length = 0
subStrBuilder.Append(str1.Substring(lastSubsBegin, (i + 1) - lastSubsBegin))
End If
End If
End If
Next
Next
subStr = subStrBuilder.ToString()
Return subStr
Catch e As Exception
Return ""
End Try
End Function
I tried it with dotnetfiddle and there it is working with your Code you posted. Please activate your warnings in your project. You have function with no return value and you return an integer or a string. This is not correct. How are you calling your function?
Here is my example I tested for you:
https://dotnetfiddle.net/mVBDQp
Your code works perfectly like Regex! As far as I can see, there is really nothing wrong with your code.
Here I even tested it under more severe case:
Public Sub Main()
Dim a As String = ""
Dim str1 As String = "Hi there this is jeff''s dog.-do you recognize this?? This__)=+ is m((a-#-&&*-ry$##! <>Hi:;? the[]{}re this|\ is jeff''s dog." 'Try to trick the logic!
Dim str2 As String = "Hi there this is jeff''s dog. ^^^^This__)=+ is m((a-#-&&*-ry$##! <>Hi:;? the[]{}re this|\ is jeff''s dog."
LongestCommonSubstring(str1, str2, a)
Console.WriteLine(a)
Console.ReadKey()
End Sub
Note that I put '-$#^_)=+&|\{}[]?!;:.<> all there. Plus I tried to trick your code by giving early result.
But the result is excellent!
You could probably put more actual samples on the inputs which give you problems. Else, you could possibly describe the environment that you use/deploy your code into. Maybe the problem lies elsewhere and not in the code.
The quickest way to solve this would be to use an escape code and replace all the ' with whatever escape code you use

Trim a string - remove space and parentheses in VB.NET

Would someone help me with this please?
In vb.net (VS2013):
a string is in the format: char12345 (6789).jpg
trim to the string to: char12345.jpg
Basically, I need to trim off the middle part: the space and everything in parentheses (including the parentheses).
will VB's trim function work? or I need to use RegEx...
Many thanks in advance!
You don't need regex, you could remove the parantheses also with pure string methods:
Dim path = "char12345 (6789).jpg"
Dim ext = IO.Path.GetExtension(path)
Dim fn = IO.Path.GetFileNameWithoutExtension(path)
Dim index = fn.IndexOf("(")
If index >= 0 Then fn = fn.Remove(index).Trim()
path = String.Format("{0}{1}", fn, ext)
Presumes that they are always directly before the extension or that the part behind them can also be removed. Otherwise it's getting a little bit more complicated:
Dim index = fn.IndexOf("(")
If index >= 0 Then
Dim endindex = fn.LastIndexOf(")", index)
If endindex >= 0 Then
fn = fn.Remove(index).Trim() & fn.Substring(endindex + 1)
Else
fn = fn.Remove(index).Trim()
End If
End If
Given your input, you can accomplish this with Split
Dim str as String = "char12345 (6789).jpg"
Console.Write(str.Split(" ")(0) & "." & str.Split(".")(1))

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

Create a replace function in vb.net

Please Help me in creating a replace function.
Problem:
Their is a alphanumeric value of any length (string) and I want to replace its all characters with 'X' except right four characters
Like :
Value : 4111111111111111
Result Should be: XXXXXXXXXXXX1111
I have created a function but got stuck:
public function myfunction(str as string)
str.Replace(str.Substring(0, str.Length - 5), 'X') 'but here I want no of x to be equals to count of length of str - 4
end function
What's a better function to perform such an operation?
Try this on for size.
Public Shared Function ObfuscateCardNumber(ByVal cardNumber As String) As String
If cardNumber.Length <= 4 Then
Return cardNumber
Else
Return cardNumber _
.Substring(cardNumber.Length - 4, 4) _
.PadLeft(cardNumber.Length, "X"c)
End If
End Function
Dim sNumber As String = "4111111111111111"
Dim sResult As String = StrDup(sNumber.Length - 4, "X"c) + Strings.Right(sNumber, 4)
something like
string result for(int i = 0;i > str.length -4;i++)
{
result = result +x
}
result = result + str.substrin(get
last 4)

How to use replace when making one variable = two others instead of just one

Okay this one might be a little tougher. I'm using VB that looks like this:
string = Replace(string.ToLower, chr(63), "A")
But I also want chr(63) = "B" as well, like this:
string = Replace(string.ToLower, chr(63), "B")
My problem is that when chr(63) is at the end of a string I need it to be B, and when it's not the end I need it to be A. I suppose that I can use an if/then/else statement. Is there a way to do this?
Example:
XXXXXchr(63)XXXXX = A
but
XXXXXXXXXXchr(63) = B
Thanks!
pseudo:
if (string[string.Length] == chr(63))
{
string[string.Length] = B
}
string = Replace(string.ToLower, chr(63), "A")
string = Replace(string.ToLower, chr(63), "A", 1, Len(string) - 1)
If Right(string, 1) = chr(63) then
Mid$(string, Len(string), 1) = 'B'
End if
Update: in response to comment:
VB String Functions
VB String Array Functions - Split, Join, Filter (very useful)
I haven't used Visual Basic since version 6, but it should be something like this:
If Robert.EndsWith(chr(63)) Then
Robert = Left(Robert, Robert.Length - 1) + "B"
End If
Then do the usual replacement with A.
This ought to do it
Dim s As String
Dim char63 As String = Convert.ToChar(63).ToString
If s.EndsWith(char63) Then
s = s.Substring(0, s.Length - 1) & "B"
End If
s = s.Replace(char63, "A")