仅通过第一次出现分隔符来分割字符串

时间:2020-01-26 21:06:00

标签: julia

我想按julia> a = "x1.y1.xyz22" "x1.y1.xyz22" julia> split(a,".") 3-element Array{SubString{String},1}: "x1" "y1" "xyz22" 的第一次出现来拆分字符串。

#include <stdio.h>

int main()
{
    char input1;
    printf("Enter a value: ");
    scanf(" %c", &input1);

    char input2;
    printf("Enter a value: ");
    scanf(" %c", &input 2);

    char A = 'A';
    char B = 'B';

    switch(input1){
        case 'A':
        printf("Your letter is A");
        break;

        case 'B':
        printf("Your letter is B");
        break;

        default: 
        printf("Invalid letter for first input! Terminating program...");
        break;
    }

    switch(input2){
        case 'A':
        printf("First letter of the alphabet");
        break;

        case 'B':
        printf("Second letter of the alphabet");
        break;

        default: 
        printf("Invalid letter for second input! Terminating program...")
        break;
    }

    return 0; 
}

在Julia中如何仅将字符串拆分一次即可: “ x1”
“ y1.xyz22”?

先谢谢您

2 个答案:

答案 0 :(得分:7)

limit关键字参数将设置结果中的最大块数:

julia> split("abc.de.fg.hij.k", "."; limit=2) # split at most once
2-element Array{SubString{String},1}:
 "abc"        
 "de.fg.hij.k"

julia> split("abc.de.fg.hij.k", "."; limit=3) # split at most twice
3-element Array{SubString{String},1}:
 "abc"     
 "de"      
 "fg.hij.k"

答案 1 :(得分:3)

使用limit关键字。

对于此类问题,您还可以有效地使用内联文档。.只需在控制台中键入?split(或任何其他函数或类型)以检索函数,参数及其常用用法的详细说明。例子。在这种情况下:

help?> split
search: split splitext splitdir splitpath splitdrive rsplit splice! displaysize @specialize @nospecialize

  split(str::AbstractString, dlm; limit::Integer=0, keepempty::Bool=true)
  split(str::AbstractString; limit::Integer=0, keepempty::Bool=false)

  Split str into an array of substrings on occurrences of the delimiter(s) dlm. dlm can be any of the formats allowed by findnext's first argument (i.e. as a string, regular expression or a function), or as a single character or collection of characters.

  If dlm is omitted, it defaults to isspace.

  The optional keyword arguments are:

    •    limit: the maximum size of the result. limit=0 implies no maximum (default)

    •    keepempty: whether empty fields should be kept in the result. Default is false without a dlm argument, true with a dlm argument.

  See also rsplit.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> a = "Ma.rch"
  "Ma.rch"

  julia> split(a,".")
  2-element Array{SubString{String},1}:
   "Ma"
   "rch"

  ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

  Splits an HyperRectangle into two along an axis at a given location.