locked
turning an array into a string RRS feed

  • Question

  • hi there

    I'm trying to turn a string array that contains string , numeric and date values

    some of the string values are in hebrew

    when turning them to a single string (using join or through a loop) the order of the values comes out wrong

    examle: strArray=(0) "1" String
      strArray=(1) "'מרכז שרות בני ברק'" String
      strArray=(2) "'01-054510'" String
      strArray=(3) "'"אשטרום בע'מ"'" String
      strArray=(4) "''" String
      strArray=(5) "#30/03/2011#" String
      strArray=(6) "'פרטי'" String
      strArray=(7) "'תל אביב'" String
      strArray=(8) "'קמינצקי 10'" String
      strArray=(9) "'99999'" String
      strArray=(10) "''" String
      strArray=(11) "''" String
      strArray=(12) "''" String
      strArray=(13) "'79-340-70'" String
      strArray=(14) "'WVWZZZ1KZAM100263'" String
      strArray=(15) "'V'" String
      strArray=(16) "'1K22E5'" String
      strArray=(17) "'ג'טה'" String
      strArray=(18) "#29/04/2010#" String
      strArray=(19) "2010" String
      strArray=(20) "2010" String
      strArray=(21) "1" String
      strArray=(22) "11" String
      strArray=(23) "8" String
      strArray=(24) "60123" String
      strArray=(25) "17" String
      strArray=(26) "'500000000010'" String
      strArray=(27) "5000" String
      strArray=(28) "0" String
      strArray=(29) "10" String
      strArray=(30) "''" String
      strArray=(31) Nothing String
      strArray=(32) "''" String
      strArray=(33) "''" String
      strArray=(34) "''" String

    comes out:

    "1,'מרכז שרות בני ברק','01-054510','"אשטרום בע'מ"','',#30/03/2011#,'פרטי','תל אביב','קמינצקי 10','99999','','','','79-340-70','WVWZZZ1KZAM100263','V','1K22E5','ג'טה',#29/04/2010#,2010,2010,1,11,8,60123,17,'500000000010',5000,0,10,'',,'','','','',"

    Please help me!

    Monday, May 16, 2011 9:50 AM

Answers

  • the thing is that when the input is in english there is no problem

    the problem comes out only when the input is in hebrew


    Danielicy

    It's due to the reversed reading order of the Hebrew strings - that was apparent from the first code example you posted and I can only assume that people who made suggestions had not tried it.  Unfortunately, I don't know why mixing strings with different reading order behaves like this, and i don't know how you can overcome it, other than redesigning things so that you are not mixing strings with different reading order. 

    • Marked as answer by Danielicy Tuesday, May 17, 2011 11:29 AM
    Tuesday, May 17, 2011 7:17 AM

All replies

  • try this:

    string.join(",", strArray)
    
    

    thanks for any help
    • Proposed as answer by XI Samuel IX Monday, May 16, 2011 12:59 PM
    • Unproposed as answer by Danielicy Monday, May 16, 2011 1:08 PM
    Monday, May 16, 2011 12:32 PM
  • try this:

    string.join(",", strArray)
    
    
    
    

    thanks for any help

    Didn't do it Paul but Tnx Anyway
    Danielicy
    Monday, May 16, 2011 12:54 PM
  • i tried it. it works perfectly:

    Public Class Form1
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strArray(34) As String
        strArray(0) = "1"
        strArray(1) = "'מרכז שרות בני ברק'"
        strArray(2) = "'01-054510'"
        strArray(3) = "'""אשטרום בע'מ""'"
        strArray(4) = "''"
        strArray(5) = "#30/03/2011#"
        strArray(6) = "'פרטי'"
        strArray(7) = "'תל אביב'"
        strArray(8) = "'קמינצקי 10'"
        strArray(9) = "'99999'"
        strArray(10) = "''"
        strArray(11) = "''"
        strArray(12) = "''"
        strArray(13) = "'79-340-70'"
        strArray(14) = "'WVWZZZ1KZAM100263'"
        strArray(15) = "'V'"
        strArray(16) = "'1K22E5'"
        strArray(17) = "'ג'טה'"
        strArray(18) = "#29/04/2010#"
        strArray(19) = "2010"
        strArray(20) = "2010"
        strArray(21) = "1"
        strArray(22) = "11"
        strArray(23) = "8"
        strArray(24) = "60123"
        strArray(25) = "17"
        strArray(26) = "'500000000010'"
        strArray(27) = "5000"
        strArray(28) = "0"
        strArray(29) = "10"
        strArray(30) = "''"
        strArray(31) = Nothing
        strArray(32) = "''"
        strArray(33) = "''"
        strArray(34) = "''"
    
    
        Debug.Print(String.Join(",", strArray))
      End Sub
    End Class
    
    

     

    result:

    1,' ','01-054510','" '"','',#30/03/2011#,'',' ',' 10','99999','','','','79-340-70','WVWZZZ1KZAM100263','V','1K22E5',''',#29/04/2010#,2010,2010,1,11,8,60123,17,'500000000010',5000,0,10,'',,'','',''


    thanks for any help
    Monday, May 16, 2011 1:28 PM
  • try this:

     

    String.Join(",", Array.ConvertAll(strArray, Function(s) "'" & s & "'"))
    
    

    thanks for any help


    the quotes have to be only around the text since the string is a value string for a sql command

    I tried your solution but still the order gets mixed up

    tnx fo the effort


    Danielicy
    Monday, May 16, 2011 1:29 PM
  • i'd recommend using parameters for your sql query instead of string concatenation
    thanks for any help
    Monday, May 16, 2011 1:30 PM
  • if you nottess in the your result  the hebrew text disapeared


    Danielicy
    • Edited by Danielicy Tuesday, May 17, 2011 11:31 AM
    Monday, May 16, 2011 2:13 PM
  • still not working for me
    Danielicy
    Monday, May 16, 2011 2:13 PM
  • on the parameters suggestion I would happily do it but the thing is the procedure recieves an unkown datatable not knowing in advance the number of columns, their names, or their datatypes so i have to build a dynamical procedure in order to recieve whatever it's being sent
    Danielicy
    Monday, May 16, 2011 2:19 PM
  • Join method is far the best option to use ro get a string out of array! Nice example paul :)
    Mitja
    Monday, May 16, 2011 2:31 PM
  • Danielicy,

    Do you have fun, Paul tries to help you but you only writes that it does not work, not one slightest piece beside the strange code you have sent.

    Are you sure you are using VB, that what you have showed does not look in the fairest way to VB.

    I think that you more are expressely are iritating this forum and have fun about that, while Paul tries in all kind of ways to help you and get only responses back from you.

    It does not work for me.


    Success
    Cor
    Monday, May 16, 2011 2:32 PM
  • Cor I'm not having any fun at all

    if you have a suggestion i'll be glad to try it

    otherwise have a nice day


    Danielicy
    Monday, May 16, 2011 2:39 PM
  • if you have any doughts cor this the whole code:

    Public Sub createtable(ByVal newtable As DataTable, ByVal filepath As String)

            Dim Cmd As OleDbCommand
            Dim SQL As String
            Dim objCmd As New OleDbCommand
            Dim Con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data source =" & filepath)
            Dim columns As String = ""
            Dim colarray(newtable.Columns.Count - 1) As String
            Dim colstr As String

            For col = 0 To newtable.Columns.Count - 1
                columns = columns & " [" & newtable.Columns(col).ColumnName.ToString & "] " & IIf(newtable.Columns(col).DataType.Name = "String", "TEXT(30),", IIf(newtable.Columns(col).DataType.Name = "Int32", "INT,", IIf(newtable.Columns(col).DataType.Name = "DateTime", "DateTime,", "")))
                colstr = colstr & newtable.Columns(col).ColumnName.ToString & ","
                colarray(col) = newtable.Columns(col).ColumnName.ToString
            Next
            colstr = Mid(colstr, 1, colstr.Length - 1)
            columns = "(" & Mid(columns, 1, columns.Length - 1) & ")"
            SQL = "CREATE TABLE " & newtable.TableName & columns
            Cmd = New OleDbCommand(SQL, Con)
            Con.Open()
            objCmd = New OleDbCommand(SQL, Con)
            objCmd.ExecuteNonQuery()

            '-----------------------------------------------------------------------
            Dim oddaTbl1 As OleDb.OleDbDataAdapter
            Dim CBTbl1 As OleDb.OleDbCommandBuilder
            Dim DTTbl1 As New DataTable

            oddaTbl1 = New OleDb.OleDbDataAdapter("select * from TBL_Source1", Con)
            CBTbl1 = New OleDb.OleDbCommandBuilder(oddaTbl1)
            oddaTbl1.Fill(DTTbl1)
            Dim DR_Tbl1NewRow As DataRow = DTTbl1.NewRow()
            Dim insertcommand As OleDbCommand
            Dim colname(newtable.Columns.Count - 1) As String

            For nt = 0 To newtable.Rows.Count - 1

                For t = 0 To colarray.Length - 1
                    DR_Tbl1NewRow(colarray(t)) = newtable.Rows(nt)(colarray(t))
                Next

                For colnum = 0 To newtable.Columns.Count - 1
                    Select Case newtable.Columns(colnum).DataType.Name
                        Case "String"
                            colname(colnum) = "'" & newtable.Rows(nt)(colarray(colnum)) & "'"

                        Case "Int32"
                            If Not IsDBNull(newtable.Rows(nt)(colarray(colnum))) Then
                                colname(colnum) = newtable.Rows(nt)(colarray(colnum))
                            End If

                        Case "DateTime"
                            colname(colnum) = "#" & newtable.Rows(nt)(colarray(colnum)) & "#"
                    End Select
                    colnum = colnum
                Next

                Dim colValstr As String '= Join(colname, ",")

                colValstr = String.Join(",", colname)

                'colValstr = String.Join(",", Array.ConvertAll(colname, Function(s) "'" & s & "'"))

                DTTbl1.Rows.Add(DR_Tbl1NewRow)
                insertcommand = New OleDbCommand("insert into TBL_Source1 (" & colstr & ") Values (" & colValstr & ")")

                For p = 0 To newtable.Columns.Count - 1
                    Select Case newtable.Columns(p).DataType.Name
                        Case "String"
                            insertcommand.Parameters.Add(colarray(p), OleDbType.Char, 50, colarray(p))
                        Case "Int32"
                            insertcommand.Parameters.Add(colarray(p), OleDbType.Integer, 10, colarray(p))
                        Case "DateTime"
                            insertcommand.Parameters.Add(colarray(p), OleDbType.Date, 10, colarray(p))
                    End Select

                Next

                oddaTbl1.InsertCommand = insertcommand
                oddaTbl1.Update(DTTbl1)
            Next
            Con.Close()
        End Sub


    Danielicy
    Monday, May 16, 2011 2:42 PM
  • Yea show what you have done so far, your problem is easy to solve, but your replies don't invite to that if you give us not the chance to help you by showing what you have done so far.

     


    Success
    Cor
    Monday, May 16, 2011 2:42 PM
  • it is supposeed to get a datatable, and copy it into an access mdb

    i'm open for suggestions


    Danielicy
    Monday, May 16, 2011 2:43 PM
  • the thing is that when the input is in english there is no problem

    the problem comes out only when the input is in hebrew


    Danielicy
    Monday, May 16, 2011 2:45 PM
  • Will you please connect your message to the one who have written that instead to your own message. Many of those longer active in the forums use threaded view so that we can see who replies to us.

    Your messages shows up as a long thread of conversation with yourself.

    If I look at your code I see nowhere a string array or even a list of string. 

    A stringArray is dimensioned like this.

    Dim myString as String() 

    What I see is a DataTable, which is a little bit strange class existing from 2 classes; datarows and datacolumns.

    If you have one datarow and you want to make from that one long string you can do something like

     

     Dim myString As String
     For i = 0 To dt.Columns.Count - 1
      Select Case dt.Columns(i).DataType
      Case GetType(System.Int32)
       myString &= CInt(theDataRow(i))
      Case GetType(System.String)
       myString &= CStr(theDataRow(i))
      Case GetType(System.DateTime)
       myString &= CDate(theDataRow(i))
      End Select
     Next
    

    Not tested of course which is impossible because we don't have your tables (and don't want to have)


    Success
    Cor

    Monday, May 16, 2011 3:19 PM
  • very clean and nice cor thanks for the tip but it still doesn't resolve my problem
    Danielicy
    Tuesday, May 17, 2011 5:02 AM
  • Than try for yourself to create a small program what should do what you want to do, that bunch of code is for nobody here usable to evaluate. 

    It should contains not more than 30 rows and be testable.

     


    Success
    Cor
    Tuesday, May 17, 2011 5:19 AM
  • well finally i found a way to overide the problem

    instead of inserting each row separatly i found out a way to copy the whole table to the database.

    heres the final and tested code:

     Public Sub createAccesstable(ByVal newtable As DataTable, ByVal filepath As String)

            Dim objCmd As New OleDbCommand
            Dim Con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data source =" & filepath)
            Dim columns As String = ""
            Dim oddaTbl1 As OleDb.OleDbDataAdapter
            Dim CBTbl1 As OleDb.OleDbCommandBuilder
            Dim DTTbl1 As New DataTable

            For col = 0 To newtable.Columns.Count - 1
                columns = columns & " [" & newtable.Columns(col).ColumnName.ToString & "] " & IIf(newtable.Columns(col).DataType.Name = "String", "TEXT(30),", IIf(newtable.Columns(col).DataType.Name = "Int32", "INT,", IIf(newtable.Columns(col).DataType.Name = "DateTime", "DateTime,", "")))
            Next
            columns = "(" & Mid(columns, 1, columns.Length - 1) & ")"
           
            Con.Open()
            objCmd = New OleDbCommand("CREATE TABLE " & newtable.TableName & columns, Con)
            objCmd.ExecuteNonQuery()
            '-----------------------------------------------------------------------
            oddaTbl1 = New OleDb.OleDbDataAdapter("select * from TBL_Source1", Con)
            CBTbl1 = New OleDb.OleDbCommandBuilder(oddaTbl1)
            oddaTbl1.Fill(DTTbl1)
            DTTbl1.Merge(newtable)

            For i = 0 To DTTbl1.Rows.Count - 1
                DTTbl1.AcceptChanges()
                DTTbl1.Rows(i).SetAdded()
                oddaTbl1.Update(DTTbl1)
            Next

            Con.Close()

        End Sub

    and it does the job although the loop takes a bit

    still if someone finds a way to deal with strings that contain both LTR and RTL text I'll be glad to know how

    thank you all for the asistance any way


    Danielicy
    Tuesday, May 17, 2011 5:48 AM
  • the thing is that when the input is in english there is no problem

    the problem comes out only when the input is in hebrew


    Danielicy

    It's due to the reversed reading order of the Hebrew strings - that was apparent from the first code example you posted and I can only assume that people who made suggestions had not tried it.  Unfortunately, I don't know why mixing strings with different reading order behaves like this, and i don't know how you can overcome it, other than redesigning things so that you are not mixing strings with different reading order. 

    • Marked as answer by Danielicy Tuesday, May 17, 2011 11:29 AM
    Tuesday, May 17, 2011 7:17 AM
  • Hi Acamar,

    Thank you for pointing the uncomfortable reply.

    To make a happy and harmony forum, I really appreciate it.

    Have a nice day.


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 9:01 AM
  • Hi Cor,

    Thank you for your contribution on the forum, I see many times your answers and reply counts is No. 1.

    I appreciate it too.

    But that reply maybe gives people a bad impression, however, we all know you absolute are not. You are a kindly and helpful man. So would you please make it more peaceful that people will do not misundstand you?

    Thank you very much.

    Have a nice day.


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.



    Tuesday, May 17, 2011 9:13 AM
  • Hi Cor,

     

    But that reply maybe gives people a bad impression, however, we all know you absolute are not. You are a kindly and helpful man. So would you please make it more peaceful that people will do not misundstand you?

     

    Mike was not angry meant, I had just fun. What Acamar wrote can be true, but I was thinking how it could become like that. But whatever, I've deleted it, probably few understand what I did mean with that text.

    :-)

     


    Thanks
    Cor



    • Edited by Cor Ligthert Tuesday, May 17, 2011 1:24 PM changed succes in thanks :-)
    Tuesday, May 17, 2011 10:41 AM
  • Well any way thank you all !

     


    Danielicy
    Tuesday, May 17, 2011 11:30 AM
  • :)


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, May 18, 2011 5:15 AM