ramakanth
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2015
06:05 AM
Reversing a String in BrightScript
What is the best way to reverse a String in BrightScript? i did reverse a sentence but not an individual word. I didn't find an equivalent of java charAt() function in brightscript for particular string.
required:
Function StrReverse(baseString as String)
revString = // logic to reverse the string
return revString
End Function
input : "roku"
output : "ukor"
apart from english text, i am pretty much interested in achieving the string reverse for arabic text, any inputs hear would be pretty helpful.
Thanks,
Ramakanth.G
required:
Function StrReverse(baseString as String)
revString = // logic to reverse the string
return revString
End Function
input : "roku"
output : "ukor"
apart from english text, i am pretty much interested in achieving the string reverse for arabic text, any inputs hear would be pretty helpful.
Thanks,
Ramakanth.G
5 REPLIES 5
sjb64
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2015
06:15 AM
Re: Reversing a String in BrightScript
The Mid function of strings to pull a single character can be used as your charat(), or string indexing that I use in c#. Use this in a for loop walking a string backward to get each char and create a new string char at a time from them would be quickest way I know to do this.
Arabic would be an challenge I believe. Numbers embedded in sentences are not reversed, and characters can change depending on position, but that might just be the way they're written, not the actual characters. I lived in Dubai for years dealing with bi-lingual sites and apps, and found manipulation of Arabic text (beyond simple display) in high level languages very difficult, so in an intermediate language like BRS it could be even more so.
Arabic would be an challenge I believe. Numbers embedded in sentences are not reversed, and characters can change depending on position, but that might just be the way they're written, not the actual characters. I lived in Dubai for years dealing with bi-lingual sites and apps, and found manipulation of Arabic text (beyond simple display) in high level languages very difficult, so in an intermediate language like BRS it could be even more so.
ramakanth
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2015
08:15 AM
Re: Reversing a String in BrightScript
Hi,
Thanks for the suggestion, can anyone provide me with the working snippet, it will be helpful
Thanks,
Ramakanth.G
Thanks for the suggestion, can anyone provide me with the working snippet, it will be helpful
Thanks,
Ramakanth.G
![Komag Komag](https://i.imgur.com/kaEvhbH.jpg)
Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2015
08:32 AM
Re: Reversing a String in BrightScript
You can see some functions in section 8 of the main BrightScript documentation, plus there are more in ifStringOps. There are multiple ways you could approach it, here's one possibility:
(results: "sdrawkcAssab")
origStr = "bassAckwards"
newStr = ""
len = Len(origStr)
FOR i = len TO 1 STEP -1
newStr = newStr + Mid(origStr, i, 1)
END FOR
? newStr
(results: "sdrawkcAssab")
ramakanth
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2015
09:22 AM
Re: Reversing a String in BrightScript
Thanks for the snippet komag
Rek
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-02-2015
03:19 PM
Re: Reversing a String in BrightScript
I'm guessing you're new to coding, so it's worth pointing out that you can put Komag's snippet into a re-usable function:
Which can then be used like this:
function stringReverse(input as String) as String
newStr = ""
len = len(input)
for i = len to 1 step -1
newStr = newStr + mid(input, i, 1)
end for
return newStr
end function
Which can then be used like this:
function main()
str = "This is my string!"
reversed = stringReverse(str)
?reversed ' Prints "!gnirts ym si sihT"
end function