Search code examples
kdb

Adding/Removing from String kdb


I was doing some work to do with kdb and have been tinkering with strings and variables. I was just wondering if its possible to remove part of a string and add something to do the end of it.

s1:"Hello"  s2:" World"

I have a joint string "Hello World" which I created using

s3:s1,s2

I was trying to remove the Hello and add something after the World in the joint string.

s3[1*til 6] = Hello

This allows me to select the Hello part of the string if this helps


Solution

  • you could use drop (_) to get rid of the "Hello" and join (,) to add on what you want. Something like

    q)6_s3,"star Hiphop"
    "Worldstar Hiphop"
    

    If you didn't want to count the letters in the first word you could use vector from scalar (vs) to get a list of enlisted words and index into it, then join onto that:

    q)(" " vs s3)[1],"star Hiphop"
    "Worldstar Hiphop"
    

    Hope this helps.