ada split()方法 [英] ada split() method

查看:127
本文介绍了ada split()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Java或C ++编写等效于split()方法的Ada.我要摄取一个字符串和一个整数,并输出两个单独的字符串值.例如: "hello"和2的拆分将返回: 第一部分是他 第二部分是llo"

I am trying to write an Ada equivalent to the split() method in Java or C++. I am to intake a string and an integer and output two seperate string values. For example: split of "hello" and 2 would return: "The first part is he and the second part is llo"

我的代码如下:

-- split.adb splits an input string about a specified position.
--
-- Input: Astring, a string,
--        Pos, an integer.
-- Precondition: pos is in Astring'Range.
-- Output: The substrings Astring(Astring'First..Pos) and
--                        Astring(Pos+1..Astring'Last).
--------------------------------------------------------------

with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Strings.Fixed;
use  Ada.Text_IO, Ada.Integer_Text_IO, Ada.Strings.Fixed;

procedure Split is

   EMPTY_STRING : String := "                                        ";

   Astring, Part1, Part2 : String := EMPTY_STRING;
   Pos, Chars_Read       : Natural;

   ------------------------------------------------
   -- Split() splits a string in two.           
   -- Receive: The_String, the string to be split,
   --          Position, the split index.        
   -- PRE: 0 < Position <= The_String.length(). 
   --     (Ada arrays are 1-relative by default)
   -- Passback: First_Part - the first substring,
   --           Last_Part - the second substring.
   ------------------------------------------------
   function Split(TheString : in String ; Pos : in Integer; Part1 : out String ; Part2     : out String)  return String is 
   begin
    Move(TheString(TheString'First .. Pos), Part1);
    Move(TheString(Pos .. TheString'Last), Part2);
    return Part1, Part2;
     end Split;



begin                                           -- Prompt for input
   Put("To split a string, enter the string: ");
   Get_Line(Astring, Chars_Read);
   Put("Enter the split position: ");
   Get(Pos);

   Split(Astring, Pos, Part1, Part2);

   Put("The first part is ");
   Put_Line(Part1);
   Put(" and the second part is ");
   Put_Line(Part2);

end Split;

我遇到的主要问题是返回两个单独的字符串值,并且通常返回整个split()函数.任何指针或帮助表示赞赏.谢谢

The main part I am having trouble with is returning the two separate string values and in general the whole split() function. Any pointers or help is appreciated. Thank you

推荐答案

考虑将Split做成具有两个out参数的procedure而不是function.然后确定PosPart1 last 索引还是Part2 first 索引;我选择了后者.

Instead of a function, consider making Split a procedure having two out parameters, as you've shown. Then decide if Pos is the last index of Part1 or the first index of Part2; I've chosen the latter.

procedure Split(
   TheString : in String; Pos : in Integer;
   Part1 : out String; Part2 : out String) is 
begin
   Move(TheString(TheString'First .. Pos - 1), Part1);
   Move(TheString(Pos .. TheString'Last), Part2);
end Split;

请注意,String索引为Positive:

type String is array(Positive range <>) of Character;
subtype Positive is Integer range 1 .. Integer'Last;

这篇关于ada split()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆