I am specifying a filename to my Fortran77 program from the command line. However, I get a newline character appended to the filename string (obtained using getarg).
How can I remove the new line character?
You can use an alternative to len_trim from https://stackoverflow.com/a/1259426/721644 adapted to find the newline character
integer function findnl(s)
character(len=*) :: s
integer i
findnl = len(s)+1
do i = 1, len(s)
if (s(i:i) .eq. achar(10)) then
findln = i
return
end if
end do
end function
After that, change the rest of the string to spaces
l = findnl(str)
if (l .le. len(str)) str(l:) = " "
Related
This is my code
local function char(...) return string.char(...) end
local function addtok(text,token,C)
text = text or ""
local string = ""
local count = 0
for word in text:gmatch(("[^%s]+"):format(char(C))) do
string = string..char(C)..word
end
string = string..char(C)..token
print(string)
end
The function
addtok("Devotion Aura;Charger;Righteous Fury","Seal of Wisdom",59)
returns
";Devotion Aura;Charger;Righteous Fury;Seal of Wisdom"
but what I want is
"Devotion Aura;Charger;Righteous Fury;Seal of Wisdom"
possible fix: print(string.sub(string, 2))
any better solution?
local function addtok(text,token,C)
local string = (text or "").. string.char(C) .. token
print(string)
end
addtok("Devotion Aura;Charger;Righteous Fury","Seal of Wisdom",59)
The line string = string..char(C)..word will prepend the first semicolon. That's the issue with appending within a loop - you have to make an exception for either the last or the first element if you don't want a trailing or leading delimiter. In this case however, you should be using table.concat, both for performance (avoiding unnecessary string copies) and convenience of joining by delimiter:
local function char(...) return string.char(...) end
local function addtok(text,token,C)
text = text or ""
local words = {}
local count = 0
for word in text:gmatch(("[^%s]+"):format(char(C))) do
table.insert(words, word)
end
local string = table.concat(words, char(C))..char(C)..token
print(string)
end
I want to extract file names from a string. The length of the string and the length of the file name are always different.
Must be done with VBA!
String:
href ist gleich: "abc/db://test.pdf|0|100">Bsp.:
I would like that:
test.pdf
I do not know how to proceed.
It would also be nice if the script could extract multiple filenames from a string.
Zb:
String:
href ist gleich: "abc//db://test.t.pdf|0|100" "db://test1.pdf|0|100">Bsp.
I would like that:
test.t.pdf test1.pdf
Sub testExtractFileName()
Debug.Print extractFileName("file://D:/ETVGI_556/Carconfigurator_file/carconf_d.pdf", "//")
Debug.Print extractFileName("abc//db://test.t.pdf|0|100")
Debug.Print extractFileName("db://test1.pdf|0|100")
End Sub
Function extractFileName(initString As String, Optional delim As String) As String
Dim necString As String
necString = left(initString, InStr(initString, ".pdf") + 3)
necString = Right(necString, Len(necString) - InStrRev(necString, _
IIf(delim <> "", delim, "/")) - IIf(delim <> "", Len(delim) - 1, 0))
extractFileName = necString
End Function
The single condition is that in front of the file name (all the time) to exist "//" characters in the initial string. And of course the file extension to all the time to be .pdf. If not, this extension is required and the function can be easily adapted...
The function returns full name if the second (optional) parameter will be "//" or just the file name (without path) if it is omitted.
One option could be using a pattern where you would match the preceding / and capture in a group 1+ word characters \w+ followed by .pdf
Your value is in capturing group 1.
/(\w+\.pdf)
See a regex demo
If you want to have a broader match than \w you could extend what you do want to match using a character class or use a negated character class [^ to match any char except the listed in the character class.
In this case the negated character class [^/|"\s] would match any char except / | " or a whitespace character \s
/([^/|"\s]+\.pdf)
See another regex demo
Try this and edit it according to your needs. At least it was designed for two of your examples.
Dim sStringToFormat As String
Dim i As Integer
Dim vSplit As Variant
Dim colFileNames As Collection
Dim sFormattedString As String
Set colFileNames = New Collection
sStringToFormat = "href ist gleich: ""abc//db://test.t.pdf|0|100"" ""db://test1.pdf|0|100"">Bsp."
vSplit = Split(sStringToFormat, "/")
For i = LBound(vSplit) To UBound(vSplit)
If InStr(vSplit(i), ".") > 0 Then
sFormattedString = Split(vSplit(i), "|")(0)
sFormattedString = Split(sFormattedString, "<")(0)
sFormattedString = Split(sFormattedString, ">")(0)
colFileNames.Add sFormattedString
End If
Next i
I'm trying to convert a Excel file into a SQL query. My problem is that there are special characters in the file I was given. I cant replace them CTRL+H because they dont show at all in the Excel file. When I write my query(either in utf8 or ANSI), they show. With Ultra-Edit, they show as HEX C2 92. With Notepad++ in utf8, they show as PU2. In ANSI, they show as Â’. I suspect it's an apostrophe. This is a french file by the way.
So far I tried to put it in a string and do these operations, but nothing worked.
Dim Line as String
Line = Wb.Worksheets(1).Cells(LineNo, ColNo)
Line = Replace(Line, "Â’", "''")
Line = Replace(Line, "’", "''")
Line = Replace(Line, "Â", "''")
Line = Replace(Line, Chr(194) & Chr(146), "''") 'decimal value of C2 92
Line = Replace(Line, Chr(146) & Chr(194), "''") 'inverted decimal value of C2 92
Thanks!
Instead of trying to eliminate various junk characters, try to focus on keeping only the good ones. Say we know valid characters are upper and lower case letters, numbers, and the underscore. This code will keep only the good ones:
Public Function KeepOnlyGood(s As String) As String
Dim CH As String, L As Long, i As Long
KeepOnlyGood = ""
L = Len(s)
For i = 1 To L
CH = Mid(s, i, 1)
If CH Like "[0-9a-zA-Z]" Or CH = "_" Then
KeepOnlyGood = KeepOnlyGood & CH
End If
Next i
End Function
If you want to replace junk with a space, the code can be modified to do just that.
I have a string which includes backspace. Displaying it to the commandline will 'apply' the backspaces such that each backspace and the non-backspace character which immediately precedes it cannot be seen:
>> tempStr = ['aaab', char(8)]
tempStr =
aaa
Yet the deletion operation operation only happens when displaying the string. It still has the backspace character, and the 'b', inside it:
>> length(tempStr)
ans =
5
I'm looking for a minimal (ideally some sort of string processing built in) function which applies the backspace operation:
>>f(tempStr)
ans =
'aaa'
It may also help to know that I have an enumerations class over the alphabet 'a' to 'z' plus ' ' and backspace (to store my own personal indexing of the letters, images associated with each etc.). It'd be real spiffy to have this backspace removal operation be a method of the superclass that acts on a vector of its objects.
You can do it with a simple function using a while loop:
function s = printb(s)
while true
% Find backspaces
I = strfind(s, char(8));
% Break condition
if isempty(I), break; end
% Remove elements
if I(1)==1
s = s(2:end);
else
s(I(1)-1:I(1)) = [];
end
end
and the test gives:
s = [char(8) 'hahaha' char(8) char(8) '!'];
numel(s) % returns 10
z = printb(s) % returns 'haha!'
numel(z) % returns 5
This is not really "minimal", but as far as my knowlegde goes I don't think this is feasible with regular expressions in Matlab.
Best,
Your problem can be solved very elegantly using regular expressions:
function newStr = applyBackspaces(tempStr)
newStr = tempStr;
while (sum(newStr==char(8))>0) % while there is at least one char(8) in newStr do:
tmp = newStr; % work on previous result
if (tmp(1) == char(8)) % if first character is char(8)
newStr = tmp(2:end); % then suppress first character
else % else delete all characters just before a char(8)
newStr = regexprep(tmp,[ '.' char(8)],''); % as well as char(8) itself.
end
end
end
In essence, what my function does is delete the character just before the backspace until there are no more backspaces in your input string tempStr.
To test if it works, we check the output and the length of the string:
>> tempStr = ['abc', char(8), 'def', char(8), char(8), 'ghi']
tempStr =
abdghi
>> length(tempStr)
ans =
12
>> applyBackspaces(tempStr)
ans =
abdghi
>> length(applyBackspaces(tempStr))
ans =
6
Hence, tempStr and applyBackspaces(tempStr) show the same string, but applyBackspaces(tempStr) is the same length as the number of characters displayed.
I have a string stored in sqlite database and I've assigned it to a var, e.g. string
string = "First line and string. This should be another string in a new line"
I want to split this string into two separated strings, the dot (.) must be replace with (\n) new line char
At the moment I'm stuck and any help would be great!!
for row in db:nrows("SELECT * FROM contents WHERE section='accounts'") do
tabledata[int] = string.gsub(row.contentName, "%.", "\n")
int = int+1
end
I tried the other questions posted here in stachoverflow but with zero luck
What about this solution:`
s = "First line and string. This should be another string in a new line"
a,b=s:match"([^.]*).(.*)"
print(a)
print(b)
Are you looking to actually split the string into two different string objects? If so maybe this can help. It's a function I wrote to add some additional functionality to the standard string library. You can use it as-is or rename it to what ever you like.
--[[
string.split (s, p)
====================================================================
Splits the string [s] into substrings wherever pattern [p] occurs.
Returns: a table of substrings or, if no match is made [nil].
--]]
string.split = function(s, p)
local temp = {}
local index = 0
local last_index = string.len(s)
while true do
local i, e = string.find(s, p, index)
if i and e then
local next_index = e + 1
local word_bound = i - 1
table.insert(temp, string.sub(s, index, word_bound))
index = next_index
else
if index > 0 and index <= last_index then
table.insert(temp, string.sub(s, index, last_index))
elseif index == 0 then
temp = nil
end
break
end
end
return temp
end
Using it is very simple, it returns a tables of strings.
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> s = "First line and string. This should be another string in a new line"
> t = string.split(s, "%.")
> print(table.concat(t, "\n"))
First line and string
This should be another string in a new line
> print(table.maxn(t))
2