Home > Back-end >  C LastDelimiter doubts
C LastDelimiter doubts

Time:09-25

I am a novice, picked up the language of time is not long, encountered in the process of learning a confusion yesterday, I want to capture a joke from a AnsiString string, use the LastDelimiter for intercepting the starting point of tag
AnsiString name=F: \ music \ kugou \ abcde mp3 songs//path name
Int a=name. LastDelimiter (" \ ");
Int b=name. LastDelimiter (". ");
Label1 - & gt; Caption:=name. The SubString (a, b);//label shows the name of the mp3, that is, I want to show abcde

Running on an Error [c + + Error] Unit1. CPP (26) : E2380 Unterminated string or character constant, one of my friends give me changed three places:
1, (" \ ") into (" \ \ ")
2, int a=name. LastDelimiter (" \ \ ") + 1;
3, Label1 - & gt; Caption=name. The SubString (a,) (b - a) + 1;
After run successfully, I was wondering, check the books, LastDelimiter return values to make the character position in the string that is the location of the "" index, where his "\ ", and the value returned to + 1, in addition the substring is not intercept the specified location is between a and b string, why expression into (a,) (b - a)?

For elder people show under the younger brother, thank you!

CodePudding user response:

C/C + +, some special characters to be at the front and a backslash "" to show the difference, such as' \ r ', '\ n', '\ 0', etc., known as the escape character, so the backslash itself should also be written '\ \', or confused
Several other problems suggest LZ carefully check this several functions was clear to show

CodePudding user response:

First in c + + is the default \ escape character, used to define a path string representation, with two \ \ characters, such as
F: \ \ music \ \ kugou \ \ abcde mp3
Unless you enable c + + 11 features, can use RAW strings, so \ said the backslash, rather than escape characters, such as
Char sz []=R "(F: \ music \ kugou \ abcde mp3)";//it is important to note that a c + + 11 or higher to support

In you the example code, a value is the location of the name string last \, a value of 15, b is the last name strings. The location of value is 21, do you want to get abcde the filename string of words, should be from the last one, the location of the next character begins to calculate, namely a + 1, and then the interception of the string length should be from the last \ to the last. The length of the is b - a - 1=5 characters, the correct code:

 AnsiString name="F: \ \ music \ \ kugou \ \ abcde mp3"; 
Int a=name. LastDelimiter (" \ \ ");
Int b=name. LastDelimiter (". ");
Label1 - & gt; Caption=name. The SubString (a + 1, b - a - 1);

CodePudding user response:

Turned out to be so, thank you, now see,
That a few functions are checked data definition, is not the same as the definition of information is so confused, the key is also to run results,

CodePudding user response:

I just tested the, if is defined as the substring (a + 1, (b - a - 1));
Come out of the result is "abcd" little e, so the launch of the substring starts cutting \ after the character, then the target string length is - a, b
And the substring if there are several kinds of expression way:
The substring (substring starting position, the substring end location)//the substring (a, b)
The substring (substring starting position, the length of the substring)//the substring (a, (b - a))
The substring (substring starting position)//the substring (a) to intercept a start at the back of the string

Problem with is the substring (a, b - a)), why can't use (a, b) intercept out?

CodePudding user response:

Use a function can?

(3) ExtractFileName ()
Prototype: extern PACKAGE AnsiString __fastcall ExtractFileName (const AnsiString FileName);

Function: drawn from the file name does not contain the path to the file name

Parameters: the FileName: to deal with the file name

Example: the ShowMessage (ExtractFileName (" c://Winnt//SOL. EXE "));//show "SOL. EXE
"

CodePudding user response:

reference 5 floor u010165006 reply:
use function can?

(3) ExtractFileName ()
Prototype: extern PACKAGE AnsiString __fastcall ExtractFileName (const AnsiString FileName);

Function: drawn from the file name does not contain the path to the file name

Parameters: the FileName: to deal with the file name

Example: the ShowMessage (ExtractFileName (" c://Winnt//SOL. EXE "));//show "SOL. EXE"


The above example is wrong, it should be:
ShowMessage (ExtractFileName (" c: \ \ Winnt \ \ SOL EXE "));//show "SOL. EXE"

CodePudding user response:

Char sz []=R "(F: \ music \ kugou \ abcde mp3)";//it is important to note that a c + + 11 or higher to support
  • Related