locked
String manipulation in VB.NET RRS feed

  • Question

  • User-809753952 posted

    Let us say I have a string like this:

     Task_Name asc ,task_no asc ,Stage_Name asc ,Priority_name asc , 

    Suppose if I want to add text PIC asc, it is allwoed.  So the string becomes   

    Task_Name asc ,task_no asc ,Stage_Name asc ,Priority_name asc , PIC asc 

    If I want to add task_no asc, it should not happen as it is already found in the string.

    But if I want to add task_no desc, then I should remove task_no asc and then add  task_no desc to string. 

    So my string becomes:  

    Task_Name asc ,task_no desc ,Stage_Name asc ,Priority_name asc , PIC asc 

    Please help me with some algorithm or sample code.

    Wednesday, October 3, 2018 11:13 AM

Answers

  • User-893317190 posted

    Hi mnmhemaj,

    You could try the code below.

     Private Function getString(ByVal origin As String, ByVal add As String) As String
            'check whether add is end with asc or desc if not it is not valid , return origin
            If Not add.TrimEnd(" ").EndsWith(" asc") And Not add.TrimEnd(" ").EndsWith(" desc") Then
    
                Return origin
            End If
    
            ' split the string into string array , for example Task_Name asc ,task_no asc  
            ' will becom ("Task_Name asc ","task_no asc")
            Dim strs As String() = origin.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
            ' check whether the array contains the string to add
            If Not origin.Contains(add.Trim(" ")) Then
    
                For index = 0 To strs.Length - 1
                    'loop through the splited string to see whether one of the elements has the same prefix
                    'if so replace it with the variable add, if not return origin + " ,"+add
                    If add.Substring(0, add.TrimEnd(" ").LastIndexOf(" ")) = strs(index).Substring(0, strs(index).TrimEnd().LastIndexOf(" ")) Then
                        strs(index) = add
                        Return String.Join(",", strs)
                    End If
    
    
                Next
                Return origin + " ," + add
            End If
            Return origin
        End Function

    My test.

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim str As String = "Task_Name asc ,task_no asc ,Stage_Name asc ,Priority_name asc"
    
            Response.Write("<span style='color:blue'>add  pic asc: </span>" + getString(str, "pic asc") + "<br/>")
            Response.Write("<span style='color:blue'>add Task_Name asc:</span>" + getString(str, "Task_Name asc") + "<br/>")
            Response.Write("<span style='color:blue'>add Task_Name desc:</span>" + getString(str, "Task_Name desc"))
    
        End Sub

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 4, 2018 8:48 AM
  • User-809753952 posted

    I solved it in another way:

    Dim sortdir As String = ""
    If GridView1.SortDirection.ToString = "Ascending" Then
    sortdir = " asc "
    ElseIf GridView1.SortDirection.ToString = "Descending" Then
    sortdir = " desc "
    End If
    If lblSort.Text <> "" Then
    Dim mystr As String = lblSort.Text
    Dim cut_at As String = GridView1.SortExpression
    
    Dim x As Integer = InStr(mystr, cut_at)
    
    If x = 0 Then
    lblSort.Text = lblSort.Text & GridView1.SortExpression & " " & sortdir
    Else
    
    Dim tobereplaced As String = mystr.Substring(x - 1, Len(cut_at) + 6)
    
    lblSort.Text = mystr.Replace(tobereplaced, "")
    lblSort.Text = lblSort.Text & GridView1.SortExpression & " " & sortdir
    End If
    
    Else
    lblSort.Text = lblSort.Text & GridView1.SortExpression & " " & sortdir
    End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 4, 2018 9:05 AM

All replies

  • User-893317190 posted

    Hi mnmhemaj,

    You could try the code below.

     Private Function getString(ByVal origin As String, ByVal add As String) As String
            'check whether add is end with asc or desc if not it is not valid , return origin
            If Not add.TrimEnd(" ").EndsWith(" asc") And Not add.TrimEnd(" ").EndsWith(" desc") Then
    
                Return origin
            End If
    
            ' split the string into string array , for example Task_Name asc ,task_no asc  
            ' will becom ("Task_Name asc ","task_no asc")
            Dim strs As String() = origin.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
            ' check whether the array contains the string to add
            If Not origin.Contains(add.Trim(" ")) Then
    
                For index = 0 To strs.Length - 1
                    'loop through the splited string to see whether one of the elements has the same prefix
                    'if so replace it with the variable add, if not return origin + " ,"+add
                    If add.Substring(0, add.TrimEnd(" ").LastIndexOf(" ")) = strs(index).Substring(0, strs(index).TrimEnd().LastIndexOf(" ")) Then
                        strs(index) = add
                        Return String.Join(",", strs)
                    End If
    
    
                Next
                Return origin + " ," + add
            End If
            Return origin
        End Function

    My test.

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim str As String = "Task_Name asc ,task_no asc ,Stage_Name asc ,Priority_name asc"
    
            Response.Write("<span style='color:blue'>add  pic asc: </span>" + getString(str, "pic asc") + "<br/>")
            Response.Write("<span style='color:blue'>add Task_Name asc:</span>" + getString(str, "Task_Name asc") + "<br/>")
            Response.Write("<span style='color:blue'>add Task_Name desc:</span>" + getString(str, "Task_Name desc"))
    
        End Sub

    The result.

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 4, 2018 8:48 AM
  • User-809753952 posted

    I solved it in another way:

    Dim sortdir As String = ""
    If GridView1.SortDirection.ToString = "Ascending" Then
    sortdir = " asc "
    ElseIf GridView1.SortDirection.ToString = "Descending" Then
    sortdir = " desc "
    End If
    If lblSort.Text <> "" Then
    Dim mystr As String = lblSort.Text
    Dim cut_at As String = GridView1.SortExpression
    
    Dim x As Integer = InStr(mystr, cut_at)
    
    If x = 0 Then
    lblSort.Text = lblSort.Text & GridView1.SortExpression & " " & sortdir
    Else
    
    Dim tobereplaced As String = mystr.Substring(x - 1, Len(cut_at) + 6)
    
    lblSort.Text = mystr.Replace(tobereplaced, "")
    lblSort.Text = lblSort.Text & GridView1.SortExpression & " " & sortdir
    End If
    
    Else
    lblSort.Text = lblSort.Text & GridView1.SortExpression & " " & sortdir
    End If

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 4, 2018 9:05 AM