2011-09-21 10 views
16

私はchar *ソースを持っています。そして、私はそれがシンボル "abc"から始まり、ソースが終わるところで終わることを知っている、それを抽出したいと思います。 strstr私はpoinerを得ることができますが、位置はありません。位置がなければ、部分文字列の長さはわかりません。純粋なCで部分文字列のインデックスを取得するにはどうすればよいですか?部分文字列のインデックスを取得

+1

あなたはおそらく、長さについて心配することなく、ポインタだけで必要なものを行うことができます。 – pmg

+0

@Country - あなたが投票できない理由はありません(周波数に限界があるかもしれません) – KevinDTimm

答えて

33

ポインタの減算を使用します。

char *str = "sdfadabcGGGGGGGGG"; 
char *result = strstr(str, "abc"); 
int position = result - str; 
int substringLength = strlen(str) - position; 
+0

oops 'char * str =" abracabcabcabcabc "' :-) – pmg

+0

ああ、彼の "source"文字列はabcで始まり、その後も続きます...--) –

+0

皆さんありがとう!何らかの理由で投票できないので、私はちょうどあなたに感謝の言葉を言うことができます – Country

6

newptr - sourceは、オフセットを与えます。

+0

ありがとうございます!私は何らかの理由で投票することができないので、私はちょうどあなたに感謝を言うことができます – Country

+0

私はあなたが投票に25の担当者を必要と思う。 – mkb

2

あなたがストリングの最初の文字へのポインタを持っている場合、およびサブストリングが、その後、元の文字列の終わりで終了:

  • strlen(substring)はあなたにその長さを与えます。
  • substring - sourceは、あなたに開始インデックスを与えます。
+0

ありがとうございます!私は何らかの理由で投票できないので、ありがとうございます。 – Country

3
char *source = "XXXXabcYYYY"; 
char *dest = strstr(source, "abc"); 
int pos; 

pos = dest - source; 
+0

oops 'source =" abracadabcabcabcabc "' :) – pmg

+0

@pmg - 重要ではありません - "abc 'で始まります" strstr ()は一度成功したら見えなくなります – KevinDTimm

+0

例をもっと完成させるためにmallocで配列を割り当てています。もちろん、私もエラーチェックを行っています;-) –

1

正式に他の人が正しいです - substring - sourceは確かに開始インデックスです。しかし、あなたはそれを必要としません:sourceへのインデックスとしてそれを使用します。したがって、コンパイラは新しいアドレスとしてsource + (substring - source)を計算しますが、ほぼすべての使用例ではsubstringで十分です。

最適化と簡素化のヒント。

+1

ありがとうございます!私はなんらかの理由で投票できません。 – Country

1

開始と終了の単語ここ

string search_string = "check_this_test"; // The string you want to get the substring 
    string from_string = "check";    // The word/string you want to start 
    string to_string = "test";    // The word/string you want to stop 

    string result = search_string;   // Sets the result to the search_string (if from and to word not in search_string) 
    int from_match = search_string.IndexOf(from_string) + from_string.Length; // Get position of start word 
    int to_match = search_string.IndexOf(to_string);       // Get position of stop word 
    if (from_match > -1 && to_match > -1)          // Check if start and stop word in search_string 
    { 
     result = search_string.Substring(from_match, to_match - from_match); // Cuts the word between out of the serach_string 
    } 
+4

C言語ではなくCに関する質問 –

+0

C++では、より簡単な方法があります:string :: Findメソッドと文字列コンストラクタstring(const string&str 、size_t pos、size_t n = npos); – Alecs

0

によって文字列から単語を切り出す機能が...オフセット機能とstrpos関数のCバージョンです

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
int strpos(char *haystack, char *needle, int offset); 
int main() 
{ 
    char *p = "Hello there all y'al, hope that you are all well"; 
    int pos = strpos(p, "all", 0); 
    printf("First all at : %d\n", pos); 
    pos = strpos(p, "all", 10); 
    printf("Second all at : %d\n", pos); 
} 


int strpos(char *hay, char *needle, int offset) 
{ 
    char haystack[strlen(hay)]; 
    strncpy(haystack, hay+offset, strlen(hay)-offset); 
    char *p = strstr(haystack, needle); 
    if (p) 
     return p - haystack+offset; 
    return -1; 
} 
関連する問題