How to trim string in MARIE? - string

Hi I'm new to Marie assembly language.
I'm trying to trim the white spaces at the end of a string. I have a print subroutine that stops printing once it reaches a 0 character so to trim the string at the ends I iterate to the end of the string, get the address of the last character and iterate backwards replacing any white spaces.
My problem is HOW to replace the white spaces because if I replace it in my trim string address I can't iterate backwards correctly? Because it loads value from address 0 instead? Any help will be appreciated.
StartRemoveSpace, LoadI TrimStringAddr //get last char that's not zero
Subt Space
Skipcond 400 //if its a space skip next line
JumpI TrimString //terminate trimming
Load CharacterReplace //replace with 0
//Replace where??
//Store TrimStringAddr
Load TrimStringAddr
Subt One // iterate backwards
Store TrimStringAddr
Jump StartRemoveSpace

Related

How do I delete the first character on a string? [Lua]

Basically, I have a string in Lua, which is a user output, but I want to trim the first character cause I don't really need it. How can I do that?
You cannot delete the first character of a string.
But you can copy a substring that starts from the second character using string.sub
string.sub(s, i [,j])
Returns the substring of s that starts at i and continues until j; i
and j can be negative. If j is absent, then it is assumed to be equal
to -1 (which is the same as the string length)....
So str = str:sub(2) will give you the substring of str that starts at character 2 which is what you want.

Use VBA to remove leading and trailing blank spaces but keeping blanks within a string

I'm trying to clean and format some set of data obtained from an accounting system and I have been able to create VBA code to use TRIM or CLEAN functions in the specific column ranges.
The thing is that I need to keep the blank spaces within the strings (can be 2, 3 or more blanks) but still remove the leading/trailing spaces and the mentioned functions reduce the inner spaces to 1. This does not work for me as the data is used as a key to match other information in further steps of the process. Bare in mind that leading/trailing blanks can be the result of space bar key, any other character that appears as a blank or even contains line breaks, so again, I want all of these removed but inner blanks. Strings can be made of alphanumeric characters.
I'm using this in a Private Sub (code is execute via a click in a button placed in the worksheet).
Dim rng1a As Range
Dim Area1a As Range
Set rng1a = Range("F2:F35001")
For Each Area1a In rng1a.Areas
Area1a.NumberFormat = "#"
Area1a.Value = Evaluate("IF(ROW(" & Area1a.Address & "),CLEAN(TRIM(" & Area1a.Address & ")))")
Next Area1a
Example (in range F2:F35001):
Original: Sample Text for Review. *(there are blanks after the string)
Result:Sample Text for Review.
Desired:Sample Text for Review.
I made some research for a couple of weeks and haven't been able to find a solution that keeps the inner blanks "as is" and avoid as much as possible duplicate question in the forum. Thanks in advance for the help.
You can do this with regular expressions:
Option Explicit
Function trimWhiteSpace(s As String) As String
Dim RE As Object
Set RE = CreateObject("vbscript.regexp")
With RE
.Global = True
.MultiLine = True
.Pattern = "^\s*(\S.*\S)\s*"
trimWhiteSpace = .Replace(s, "$1")
End With
End Function
Explanation of the Regex
Trim leading and trailing white space
^\s*(\S.*\S)\s*
Options: Case sensitive; ^$ match at line breaks
Assert position at the beginning of a line (at beginning of the string or after a line break character) (line feed, line feed, line separator, paragraph separator) ^
Match a single character that is a “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) \s*
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
Match the regex below and capture its match into backreference number 1 (\S.*\S)
Match a single character that is NOT a “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) \S
Match any single character that is NOT a line break character (line feed) .*
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
Match a single character that is NOT a “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) \S
Match a single character that is a “whitespace character” (ASCII space, tab, line feed, carriage return, vertical tab, form feed) \s*
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
$1
Insert the text that was last matched by capturing group number 1 $1
Created with RegexBuddy
On the other hand, if you want to avoid regular expressions, and if your only leading/trailing "white-space" characters are space, tab and linefeed, AND if the only "internal" white space characters are the space, you could use:
Function trimWhiteSpace(s As String) As String
trimWhiteSpace = Trim(Replace(Replace([a1], vbLf, ""), vbTab, ""))
End Function
Note that the VBA Trim function (unlike the worksheet function), only removes leading and trailing spaces, and leaves internal spaces unchanged. But this won't work if you have tab's within the string that need to be preserved.
Either of the above can be incorporated into your macro.
Have you tried using the LTRIM function to remove leading spaces then RTRIM to remove the trailing ones which will leave the internal ones intact?
From your description you don't expect TAB characters or Carriage Returns in the middle of your strings so you could just do a replace for them:
strSource = Replace(strSource, vbTab, "")
strSource = Replace(strSource, vbCrLf, " ")

How to trim white spaces at the end of a string in Marie?

Hi I'm new to Marie programming language and I have a string with the address NameAddr.
My print subroutine stops printing when it reaches a 0 character (marking the end of the string).So to trim the white spaces I simply iterate backwards from the address of the last character and as long as its a white space I replace it with a 0.
However, my removeSpace subroutine does not terminate and when I step through it its not updating the LastCharAddr properly after I pass in the address of the last character?
//Remove spaces
LastCharAddr, HEX 0
RemoveSpace, HEX 0
Space, DEC 32 //constant needed for subroutine
CharacterReplace, Hex 000 //constant for subroutine
StartRemoveSpace, LoadI LastCharAddr
Subt Space
Skipcond 400 //If its a space
JumpI RemoveSpace //if not a space terminate
Load CharacterReplace //replace with 0
Store LastCharAddr //Replace
Load LastCharAddr
Subt One// iterate backwards
Store LastCharAddr
Jump StartRemoveSpace
Any help will be appreciated thanks!

How to remove extra spaces in between string, matlab?

I have created a script to convert text to morsecode, and now I want to modify it to include a slash between words.So something like space slash space between morsecode words. I know my loop before the main loop is incorrect and I want to fix it to do as stated before I just really need help Thank You!!!:
...
Word=input('Please enter a word:','s');
...
Code=MC_1;
...
case ' '
Code='/'
otherwise
Valid=0;
end
if Valid
fprintf('%s ',Code);
else
disp('Input has invalid characters!')
break
end
I know you want to write a loop to remove multiple spaces in between words, but the best way to remove white space in your particular problem would be to use regular expressions, specifically with regexprep. Regular expressions are used to search for particular patterns / substrings within a larger string. In this case, what we are trying to find are substrings that consist of more than one whitespace. regexprep finds substrings that match a pattern, and replaces them with another string. In our case, you would search for any substrings within your string that contain at least one more whitespace characters, and replace them with a single whitespace character. Also, I see that you've trimmed both leading and trailing whitespace for the string using strtrim, which is great. Now, all you need to do is callregexprep like so:
Word = regexprep(Word, '\s+', ' ');
\s+ is the regular expression for finding at least one white space character. We then replace this with a single whitespace. As such, supposing we had this string stored in Word:
Word = ' hello how are you ';
Doing a trim of leading and trailing whitespace, then calling regexprep in the way we talked about thus gives:
Word = strtrim(Word);
Word = regexprep(Word, '\s+', ' ')
Word =
hello how are you
As you can see, the leading and trailing white space was removed with strtrim, and the regular expression takes care of the rest of the spaces in between.
However, if you are dead set on using a loop, what you can do is use a logical variable which is set to true when we detect a white space, and then we use this variable and skip other white space characters until we hit a character that isn't a space. We would then place our space, then /, then space, then continue. In other words, do something like this:
Word = strtrim(Word); %// Remove leading and trailing whitespace
space_hit = false; %// Initialize space encountered flag
Word_noSpace = []; %// Will store our new string
for index=1:length(Word) %// For each character in our word
if Word(index) == ' ' %// If we hit a space
if space_hit %// Check to see if we have already hit a space
continue; %// Continue if we have
else
Word_noSpace = [Word_noSpace ' ']; %// If not, add a space, then set the flag
space_hit = true;
end
else
space_hit = false; %// When we finally hit a non-space, set back to false
Word_noSpace = [Word_noSpace Word(index)]; %// Keep appending characters
end
end
Word = Word_noSpace; %// Replace to make compatible with the rest of your code
for Character = Word %// Your code begins here
...
...
What the above code does is that we have an empty string called Word_noSpace that will contain our word with no extra spaces, and those spaces replaced with a single whitespace. The loop goes through each character, and should we encounter a space, we check to see if we have already encountered a space. If we have, just continue on in the loop. If we haven't, then concatenate a whitespace. Once we finally hit a non-space character, we simply just add those characters that are not spaces to this new string. The result will be a string with no extra spaces, and those are replaced with a single white space.
Running the above code after you trim the leading and trailing white space thus gives:
Word =
hello how are you

Adding blank spaces into a string in C#

I have this code
String coNum = customerOrderLine.coNum.PadLeft(10 - customerOrderLine.coNum.Length);
I know that customerOrderLine.coNum = "123456" So I should end up with coNum being having 4 empty spaces at the front of it but I end up with it being "123456". How do I fix this? I tried PadRight in case that was the mistake and it also failed to work. I have to have the 4 empty spaces at the beginning to pass it into the API I am working on or it will fail.
PadLeft takes a total length as a parameter, so I think you want
String coNum = customerOrderLine.coNum.PadLeft(10);
This is because you have incorrectly specified the totalWidth parameter of the Pad* method.
From docs:
The number of characters in the resulting string, equal to the number
of original characters plus any additional padding characters.[...] If totalWidth is equal to the length of this instance, the method
returns a new string that is identical to this instance.
PadLeft does not specify a default character to pad with; your second argument should be the character to use for the pad, i.e.:
String coNum = customerOrderLine.coNum.PadLeft(10, ' ');
Edit: Also the first argument should be total desired length, not number of pad characters to add, per #Matthew's answer.

Resources