How can I do an indexOf() in Lotus Notes Formula language (# commands)? - string

I can't find this anywhere in the Domino Designer help. It seems so straightforward!
All I need to do is find the position of a character in a string.

You could use #Left or #Leftback. I think in this case they work the same.
src:= {your field value to search};
char:= {your target character};
indexof:= #Length(#Left(src;char))

searchResult:=#Left(SearchString;"C");
indexOf:=#If(searchResult="";0;#Length(searchResult));
indexOf

(edited) Please see the answer from charles ross instead.
https://stackoverflow.com/a/19437044/11293
My less efficient method is below.
If you really need the character position though you could do this:
REM {
S Source string
F Character to find
R Location of character in string or 0
};
S := "My string";
F := "t";
LEN_S := #Length(S);
R := 0;
#For(I := 1; I < LEN_S; I := I + 1;
#If(#Middle(S; I; 1) = F;
#Do(R := I; I := LEN_S);
#Nothing
)
);

#Length(src) - #Length(#ReplaceSubstring(src;srch;""))

Related

Longest prefix+suffix-combination in set of strings

I have a set of strings (less than 30) of length 1 to ~30. I need to find the subset of at least two strings that share the longest possible prefix- + suffix-combination.
For example, let the set be
Foobar
Facar
Faobaron
Gweron
Fzobar
The prefix/suffix F/ar has a combined length of 3 and is shared by Foobar, Facar and Fzobar; the prefix/suffix F/obar has a combined length of 5 and is shared by Foobar and Fzobar. The searched-for prefix/suffix is F/obar.
Note that this is not to be confused with the longest common prefix/suffix, since only two or more strings from the set need to share the same prefix+suffix. Also note that the sum of the lengths of both the prefix and the suffix is what is to be maximized, so both need to be taken into account. The prefix or suffix may be the empty string.
Does anyone know of an efficient method to implement this?
How about this:
maxLen := -1;
for I := 0 to Len(A) - 1 do
if Len(A[I]) > maxLen then // (1)
for J := 0 to Len(A[I]) do
for K := 0 to Len(A[I]) - J do
if J+K > maxLen then // (2)
begin
prf := LeftStr(A[I], J);
suf := RightStr(A[I], K);
found := False;
for m := 0 to Len(sufList) - 1 do
if (sufList[m] = suf) and (prfList[m] = prf) then
begin
maxLen := J+K;
Result := prf+'/'+suf;
found := True;
// (3)
n := 0;
while n < Len(sufList) do
if Len(sufList[n])+Len(prfList[n]) <= maxLen then
begin
sufList.Delete(n);
prfList.Delete(n);
end
else
Inc(n);
// (end of 3)
Break;
end;
if not found then
begin
sufList.Add(suf);
prfList.Add(prf);
end;
end;
In this example maxLen keeps sum of lengths of longest found prefix/suffix so far. The most important part of it is the line marked with (2). It bypasses lots of unnecessary string comparisons. In section (3) it eliminates any existing prefix/suffix that is shorter than newly found one (winch is duplicated).

How can I get a char of a String in delphi 7?

This is something that should be easey but I just can´t get it work.
I come from java so maby I have a error in my thinking here.
What I want to do is that I have a string with two letters like 't4' or 'pq'.
Now I just want to get each of the chracters in the string as an own string.
So I do:
firstString := myString[0];
but I don´t even get this compiled.
So I figured that they start counting form 1 and put 1 as an index.
Now I do this in a while loop and the first time I go through it it works fine. Then the second time the results are just empty or wrong numbers.
What am I missing here?
(I also tried copy but that doesn´t work either!)
while i < 10 do
begin
te := 'te';
a := te[1];
b := te[2];
i := i +1;
end;
the first loop a is 't' and b is 'e' as I would expect. The second time a is '' and b ist 't' which I don´t understand!
Strings are 1-based, not zero-based. Try the following, after adding StrUtils to your Uses list (for DupeString):
var
MyString : String;
begin
MyString := '12345';
Caption := StringOfChar(MyString[1], 8) + ':' + DupeString(Copy(MyString, 3, 2), 4);
You could split it up to mke it easier to follow, of course:
var
MyString,
S1,
S2,
S3: String;
begin
MyString := '12345';
S1 := StringOfChar(MyString[1], 8);
S2 := Copy(MyString, 3, 2);
S3 := DupeString(S2, 4);
Caption := S1 + ':' + S3;

Storing string references

Problem
There are multiple ways to store string reference, so how would you do it in the example code? Currently the problem is with storing access to string because it is causing non-local pointer cannot point to local object. Is storing 'First and 'Last to reference a string a preferable way?
String reference storage
This record stores reference to a string. The First and Last is supposed to point to a string. The Name should be able to the same I think, but that will cause non-local pointer cannot point to local object when a local string is assigned to that. So the current work around solution is to use First and Last.
type Segment is record
First : Positive;
Last : Positive;
Length : Natural := 0;
Name : access String;
end record;
Assigning sub string reference
The commented line is causing non-local pointer cannot point to local object. This is because Item is local. Source is not local and that is the string I want sub string references from.
procedure Find (Source : aliased String; Separator : Character; Last : out Natural; Item_Array : out Segment_Array) is
P : Positive := Source'First;
begin
for I in Item_Array'Range loop
declare
Item : aliased String := Separated_String_Next (Source, Separator, P);
begin
exit when Item'Length = 0;
Item_Array (I).Length := Item'Length;
Item_Array (I).First := Item'First;
Item_Array (I).Last := Item'Last;
--Item_Array (I).Name := Item'Access;
Last := I;
end;
end loop;
end;
Example
with Ada.Text_IO;
with Ada.Integer_Text_IO;
procedure Main is
use Ada.Text_IO;
use Ada.Integer_Text_IO;
function Separated_String_Next (Source : String; Separator : Character; P : in out Positive) return String is
A : Positive := P;
B : Positive;
begin
while A <= Source'Last and then Source(A) = Separator loop
A := A + 1;
end loop;
P := A;
while P <= Source'Last and then Source(P) /= Separator loop
P := P + 1;
end loop;
B := P - 1;
while P <= Source'Last and then Source(P) = Separator loop
P := P + 1;
end loop;
return Source (A .. B);
end;
type Segment is record
First : Positive;
Last : Positive;
Length : Natural := 0;
Name : access String;
end record;
type Segment_Array is array (Integer range <>) of Segment;
procedure Find (Source : String; Separator : Character; Last : out Natural; Item_Array : out Segment_Array) is
P : Positive := Source'First;
begin
for I in Item_Array'Range loop
declare
Item : aliased String := Separated_String_Next (Source, Separator, P);
begin
exit when Item'Length = 0;
Item_Array (I).Length := Item'Length;
Item_Array (I).First := Item'First;
Item_Array (I).Last := Item'Last;
--Item_Array (I).Name := Item'Access;
Last := I;
end;
end loop;
end;
Source : String := ",,Item1,,,Item2,,Item3,,,,,,";
Item_Array : Segment_Array (1 .. 100);
Last : Natural;
begin
Find (Source, ',', Last, Item_Array);
Put_Line (Source);
Put_Line ("Index First Last Name");
for I in Item_Array (Item_Array'First .. Last)'Range loop
Put (I, 5);
Put (Item_Array (I).First, 6);
Put (Item_Array (I).Last, 5);
Put (" ");
Put (Source (Item_Array (I).First .. Item_Array (I).Last));
New_Line;
end loop;
end;
Output
,,Item1,,,Item2,,Item3,,,,,,
Index First Last Name
1 3 7 Item1
2 11 15 Item2
3 18 22 Item3
The error message tells you exactly what is wrong : Item is a string declared locally, i.e. on the stack, and you are assigning its address to an access type (pointer). I hope I don't need to explain why that won't work.
The immediate answer - which isn't wrong but isn't best practice either, is to allocate space for a new string - in a storage pool or on the heap - which is done with new.
Item : access String := new String'(Separated_String_Next (Source, Separator, P));
...
Item_Array (I).Name := Item;
Note that some other record members, at least, Length all appear to be completely redundant since it is merely a copy of its eponymous attributes, so should probably be eliminated (unless there's a part of the picture I can't see).
There are better answers. Sometimes you need to use access types, and handle their object lifetimes and all the ways they can go wrong. But more often their appearance is a hint that something in the design can be improved : for example:
the Unbounded_String may manage your strings more simply
You could use the length as a discriminant on the Segment record, and store the actual string (not an Access) in the record itself
Ada.Containers are a standard library of containers to abstract over handling the storage yourself (much as the STL is used in C++).
If you DO decide you need access types, it's better to use a named access type type Str_Access is access String; - then you can create a storage pool specific to Str_Acc types, and release the entire pool in one operation, to simplify object lifetime management and eliminate memory leaks.
Note the above essentially "deep copies" the slices of the Source string. If there is a specific need to "shallow copy" it - i.e. refer to the specific substrings in place - AND you can guarantee its object lifetime, this answer is not what you want. If so, please clarify the intent of the question.
For a "shallow copy" the approach in the question essentially fails because Item is already a deep copy ... on the stack.
The closest approach I can see is to make the source string aliassed ... you MUST do as you want each Segment to refer to it ... and pass its access to the Find procedure.
Then each Segment becomes a tuple of First, Last, (redundant Length) and access to the entire string (rather than a substring).
procedure Find (Source : access String; Separator : Character;
Last : out Natural; Item_Array : out Segment_Array) is
P : Positive := Source'First;
begin
for I in Item_Array'Range loop
declare
Item : String := Separated_String_Next (Source.all, Separator, P);
begin
exit when Item'Length = 0;
...
Item_Array (I).Name := Source;
Last := I;
end;
end loop;
end;
Source : aliased String := ",,Item1,,,Item2,,Item3,,,,,,";
...
Find (Source'access, ',', Last, Item_Array);
for I in Item_Array (Item_Array'First .. Last)'Range loop
...
Put (Item_Array (I).Name(Item_Array (I).First .. Item_Array (I).Last));
New_Line;
end loop;
A helper to extract a string from a Segment would probably be useful:
function get(S : Segment) return String is
begin
return S.Name(S.First .. S.Last);
end get;
...
Put (get(Item_Array (I));
The only rationale I can see for such a design is where the set of strings to be parsed or dissected will barely fit in memory so duplication must be avoided. Perhaps also embedded programming or some such discipline where dynamic (heap) allocation is discouraged or even illegal.
I see no solution involving address arithmetic within a string, since an array is not merely its contents - if you point within it, you lose the attributes. You can make the same criticism of the equivalent C design : you can identify the start of a substring with a pointer, but you can't just stick a null terminator at the end of the substring without breaking the original string.
Given the bigger picture ... what you need, rather than the low level details of how you want to achieve it, there are probably better solutions.

How to get the last X Characters of a Golang String?

If I have the string "12121211122" and I want to get the last 3 characters (e.g. "122"), is that possible in Go? I've looked in the string package and didn't see anything like getLastXcharacters.
You can use a slice expression on a string to get the last three bytes.
s := "12121211122"
first3 := s[0:3]
last3 := s[len(s)-3:]
Or if you're using unicode you can do something like:
s := []rune("世界世界世界")
first3 := string(s[0:3])
last3 := string(s[len(s)-3:])
Check Strings, bytes, runes and characters in Go and Slice Tricks.
The answer depends on what you mean by "characters". If you mean bytes then:
s := "12121211122"
lastByByte := s[len(s)-3:]
If you mean runes in a utf-8 encoded string, then:
s := "12121211122"
j := len(s)
for i := 0; i < 3 && j > 0; i++ {
_, size := utf8.DecodeLastRuneInString(s[:j])
j -= size
}
lastByRune := s[j:]
You can also convert the string to a []rune and operate on the rune slice, but that allocates memory.

Check if a string contains a word but only in specific position?

How can I check if a string contains a substring, but only in a specific position?
Example string:
What is your favorite color? my [favorite] color is blue
If I wanted to check if the string contained a specific word I usually do this:
var
S: string;
begin
S := 'What is your favorite color? my [favorite] color is blue';
if (Pos('favorite', S) > 0) then
begin
//
end;
end;
What I need is to determine if the word favorite exists in the string, ignoring though if it appears inside the [ ] symbols, which the above code sample clearly does not do.
So if we put the code into a boolean function, some sample results would look like this:
TRUE: What is your favorite color? my [my favorite] color is blue
TRUE: What is your favorite color? my [blah blah] color is blue
FALSE: What is your blah blah color? my [some favorite] color is blue
The first two samples above are true because the word favorite is found outside of the [ ] symbols, whether it is inside them or not.
The 3rd sample is false because even though there is the word favorite, it only appears inside the [ ] symbols - we should only check if it exists outside of the symbols.
So I need a function to determine whether or not a word (favorite in this example) appears in a string, but ignoring the fact if the word is surrounded inside [ ] symbols.
I like Sertac's idea about deleting strings enclosed by brackets and searching for a string after that. Here is a code sample extended by a search for whole words and case sensitivity:
function ContainsWord(const AText, AWord: string; AWholeWord: Boolean = True;
ACaseSensitive: Boolean = False): Boolean;
var
S: string;
BracketEnd: Integer;
BracketStart: Integer;
SearchOptions: TStringSearchOptions;
begin
S := AText;
BracketEnd := Pos(']', S);
BracketStart := Pos('[', S);
while (BracketStart > 0) and (BracketEnd > 0) do
begin
Delete(S, BracketStart, BracketEnd - BracketStart + 1);
BracketEnd := Pos(']', S);
BracketStart := Pos('[', S);
end;
SearchOptions := [soDown];
if AWholeWord then
Include(SearchOptions, soWholeWord);
if ACaseSensitive then
Include(SearchOptions, soMatchCase);
Result := Assigned(SearchBuf(PChar(S), StrLen(PChar(S)), 0, 0, AWord,
SearchOptions));
end;
Here is an optimized version of the function, which uses pointer char iteration without string manipulation. In comparison with a previous version this handles the case when you have a string with missing closing bracket like for instance My [favorite color is. Such string is there evaluated to True because of that missing bracket.
The principle is to go through the whole string char by char and when you find the opening bracket, look if that bracket has a closing pair for itself. If yes, then check if the substring from the stored position until the opening bracket contains the searched word. If yes, exit the function. If not, move the stored position to the closing bracket. If the opening bracket doesn't have own closing pair, search for the word from the stored position to the end of the whole string and exit the function.
For commented version of this code follow this link.
function ContainsWord(const AText, AWord: string; AWholeWord: Boolean = True;
ACaseSensitive: Boolean = False): Boolean;
var
CurrChr: PChar;
TokenChr: PChar;
TokenLen: Integer;
SubstrChr: PChar;
SubstrLen: Integer;
SearchOptions: TStringSearchOptions;
begin
Result := False;
if (Length(AText) = 0) or (Length(AWord) = 0) then
Exit;
SearchOptions := [soDown];
if AWholeWord then
Include(SearchOptions, soWholeWord);
if ACaseSensitive then
Include(SearchOptions, soMatchCase);
CurrChr := PChar(AText);
SubstrChr := CurrChr;
SubstrLen := 0;
while CurrChr^ <> #0 do
begin
if CurrChr^ = '[' then
begin
TokenChr := CurrChr;
TokenLen := 0;
while (TokenChr^ <> #0) and (TokenChr^ <> ']') do
begin
Inc(TokenChr);
Inc(TokenLen);
end;
if TokenChr^ = #0 then
SubstrLen := SubstrLen + TokenLen;
Result := Assigned(SearchBuf(SubstrChr, SubstrLen, 0, 0, AWord,
SearchOptions));
if Result or (TokenChr^ = #0) then
Exit;
CurrChr := TokenChr;
SubstrChr := CurrChr;
SubstrLen := 0;
end
else
begin
Inc(CurrChr);
Inc(SubstrLen);
end;
end;
Result := Assigned(SearchBuf(SubstrChr, SubstrLen, 0, 0, AWord,
SearchOptions));
end;
In regular expressions, there is a thing called look-around you could use. In your case you can solve it with negative lookbehind: you want "favorite" unless it's preceded with an opening bracket. It could look like this:
(?<!\[[^\[\]]*)favorite
Step by step: (?<! is the negative lookbehind prefix, we're looking for \[ optionally followed by none or more things that are not closing or opening brackets: [^\[\]]*, close the negative lookbehind with ), and then favorite right after.
I think you can reword your problem as "find an ocurrence of the provided string not being surrounded by square brackets." If that describes your issue, then you can go ahead and use a simple regular expression like [^\[]favorite[^\]].

Resources