WeixiangYan/CodeTransOcean huggingface dataset:
Task: Narcissistic decimal number



C#: using System;

namespace Narcissistic
{
    class Narcissistic
    {
        public bool isNarcissistic(int z)
        {
            if (z < 0) return false;
            string n = z.ToString();
            int t = 0, l = n.Length;
            foreach (char c in n)
                t += Convert.ToInt32(Math.Pow(Convert.ToDouble(c - 48), l));

            return t == z;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Narcissistic n = new Narcissistic();
            int c = 0, x = 0;
            while (c < 25)
            {
                if (n.isNarcissistic(x))
                {
                    if (c % 5 == 0) Console.WriteLine();
                    Console.Write("{0,7} ", x);
                    c++;
                }
                x++;
            }
            Console.WriteLine("\n\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}







COBOL:        PROGRAM-ID. NARCISSIST-NUMS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
 
           01 num-length PIC 9(2) value 0.
           01 in-sum PIC  9(9) value 0.
           01 counter PIC  9(9) value 0.
           01 current-number PIC  9(9) value 0.
           01 narcissist PIC Z(9).
           01 temp PIC  9(9) value 0.
           01 modulo PIC  9(9) value 0.
           01 answer PIC  9 .
 
       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "the first 20 narcissist numbers:" .
 
           MOVE 20 TO counter.
           PERFORM UNTIL counter=0
 
               PERFORM 000-NARCISSIST-PARA
 
                   IF answer = 1 
                       SUBTRACT 1 from counter
                       GIVING counter
                       MOVE current-number TO narcissist
                       DISPLAY narcissist
                   END-IF
 
                   ADD 1 TO current-number
 
               END-PERFORM
 
            STOP RUN.
 
       000-NARCISSIST-PARA.
 
             MOVE ZERO TO in-sum.
             MOVE current-number TO temp.
             COMPUTE num-length =1+  FUNCTION Log10(temp)
 
             PERFORM  UNTIL temp=0
 
                  DIVIDE temp BY 10 GIVING temp
                            REMAINDER  modulo
 
                  COMPUTE modulo=modulo**num-length
                  ADD modulo to in-sum GIVING in-sum
 
            END-PERFORM.
 
               IF current-number=in-sum
                   MOVE 1 TO answer
                   ELSE MOVE 0 TO answer
               END-IF.
 
       END PROGRAM NARCISSIST-NUMS.
Task: Narcissistic decimal number


C++: #include <iostream>
#include <vector>
using namespace std;
typedef unsigned int uint;

class NarcissisticDecs
{
public:
    void makeList( int mx )
    {
	uint st = 0, tl; int pwr = 0, len;
        while( narc.size() < mx )
	{
	    len = getDigs( st );
	    if( pwr != len )
	    {
		pwr = len;
		fillPower( pwr );
	    }
            tl = 0;
	    for( int i = 1; i < 10; i++ )
		tl += static_cast<uint>( powr[i] * digs[i] );

	    if( tl == st ) narc.push_back( st );
	    st++;
	}
    }

    void display()
    {
	for( vector<uint>::iterator i = narc.begin(); i != narc.end(); i++ )
	    cout << *i << " ";
	cout << "\n\n";
    }

private:
    int getDigs( uint st )
    {
	memset( digs, 0, 10 * sizeof( int ) );
	int r = 0;
	while( st )
	{
	    digs[st % 10]++;
	    st /= 10;
	    r++;
	}
        return r;
    }

    void fillPower( int z )
    {
	for( int i = 1; i < 10; i++ )
	    powr[i] = pow( static_cast<float>( i ), z );
    }

    vector<uint> narc;
    uint powr[10];
    int digs[10];
};

int main( int argc, char* argv[] )
{
    NarcissisticDecs n;
    n.makeList( 25 );
    n.display();
    return system( "pause" );
}








COBOL:        PROGRAM-ID. NARCISSIST-NUMS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
 
           01 num-length PIC 9(2) value 0.
           01 in-sum PIC  9(9) value 0.
           01 counter PIC  9(9) value 0.
           01 current-number PIC  9(9) value 0.
           01 narcissist PIC Z(9).
           01 temp PIC  9(9) value 0.
           01 modulo PIC  9(9) value 0.
           01 answer PIC  9 .
 
       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "the first 20 narcissist numbers:" .
 
           MOVE 20 TO counter.
           PERFORM UNTIL counter=0
 
               PERFORM 000-NARCISSIST-PARA
 
                   IF answer = 1 
                       SUBTRACT 1 from counter
                       GIVING counter
                       MOVE current-number TO narcissist
                       DISPLAY narcissist
                   END-IF
 
                   ADD 1 TO current-number
 
               END-PERFORM
 
            STOP RUN.
 
       000-NARCISSIST-PARA.
 
             MOVE ZERO TO in-sum.
             MOVE current-number TO temp.
             COMPUTE num-length =1+  FUNCTION Log10(temp)
 
             PERFORM  UNTIL temp=0
 
                  DIVIDE temp BY 10 GIVING temp
                            REMAINDER  modulo
 
                  COMPUTE modulo=modulo**num-length
                  ADD modulo to in-sum GIVING in-sum
 
            END-PERFORM.
 
               IF current-number=in-sum
                   MOVE 1 TO answer
                   ELSE MOVE 0 TO answer
               END-IF.
 
       END PROGRAM NARCISSIST-NUMS.
Task: Narcissistic decimal number





Java: public class Narc{
	public static boolean isNarc(long x){
		if(x < 0) return false;
		
		String xStr = Long.toString(x);
		int m = xStr.length();
		long sum = 0;
		
		for(char c : xStr.toCharArray()){
			sum += Math.pow(Character.digit(c, 10), m);
		}
		return sum == x;
	}

	public static void main(String[] args){
		for(long x = 0, count = 0; count < 25; x++){
			if(isNarc(x)){
				System.out.print(x + " ");
				count++;
			}
		}
	}
}





COBOL:        PROGRAM-ID. NARCISSIST-NUMS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
 
           01 num-length PIC 9(2) value 0.
           01 in-sum PIC  9(9) value 0.
           01 counter PIC  9(9) value 0.
           01 current-number PIC  9(9) value 0.
           01 narcissist PIC Z(9).
           01 temp PIC  9(9) value 0.
           01 modulo PIC  9(9) value 0.
           01 answer PIC  9 .
 
       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "the first 20 narcissist numbers:" .
 
           MOVE 20 TO counter.
           PERFORM UNTIL counter=0
 
               PERFORM 000-NARCISSIST-PARA
 
                   IF answer = 1 
                       SUBTRACT 1 from counter
                       GIVING counter
                       MOVE current-number TO narcissist
                       DISPLAY narcissist
                   END-IF
 
                   ADD 1 TO current-number
 
               END-PERFORM
 
            STOP RUN.
 
       000-NARCISSIST-PARA.
 
             MOVE ZERO TO in-sum.
             MOVE current-number TO temp.
             COMPUTE num-length =1+  FUNCTION Log10(temp)
 
             PERFORM  UNTIL temp=0
 
                  DIVIDE temp BY 10 GIVING temp
                            REMAINDER  modulo
 
                  COMPUTE modulo=modulo**num-length
                  ADD modulo to in-sum GIVING in-sum
 
            END-PERFORM.
 
               IF current-number=in-sum
                   MOVE 1 TO answer
                   ELSE MOVE 0 TO answer
               END-IF.
 
       END PROGRAM NARCISSIST-NUMS.
Task: Narcissistic decimal number







Python: from __future__ import print_function
from itertools import count, islice

def narcissists():
    for digits in count(0):
        digitpowers = [i**digits for i in range(10)]
        for n in range(int(10**(digits-1)), 10**digits):
            div, digitpsum = n, 0
            while div:
                div, mod = divmod(div, 10)
                digitpsum += digitpowers[mod]
            if n == digitpsum:
                yield n

for i, n in enumerate(islice(narcissists(), 25), 1):
    print(n, end=' ')
    if i % 5 == 0: print() 
print()



COBOL:        PROGRAM-ID. NARCISSIST-NUMS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
 
           01 num-length PIC 9(2) value 0.
           01 in-sum PIC  9(9) value 0.
           01 counter PIC  9(9) value 0.
           01 current-number PIC  9(9) value 0.
           01 narcissist PIC Z(9).
           01 temp PIC  9(9) value 0.
           01 modulo PIC  9(9) value 0.
           01 answer PIC  9 .
 
       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "the first 20 narcissist numbers:" .
 
           MOVE 20 TO counter.
           PERFORM UNTIL counter=0
 
               PERFORM 000-NARCISSIST-PARA
 
                   IF answer = 1 
                       SUBTRACT 1 from counter
                       GIVING counter
                       MOVE current-number TO narcissist
                       DISPLAY narcissist
                   END-IF
 
                   ADD 1 TO current-number
 
               END-PERFORM
 
            STOP RUN.
 
       000-NARCISSIST-PARA.
 
             MOVE ZERO TO in-sum.
             MOVE current-number TO temp.
             COMPUTE num-length =1+  FUNCTION Log10(temp)
 
             PERFORM  UNTIL temp=0
 
                  DIVIDE temp BY 10 GIVING temp
                            REMAINDER  modulo
 
                  COMPUTE modulo=modulo**num-length
                  ADD modulo to in-sum GIVING in-sum
 
            END-PERFORM.
 
               IF current-number=in-sum
                   MOVE 1 TO answer
                   ELSE MOVE 0 TO answer
               END-IF.
 
       END PROGRAM NARCISSIST-NUMS.
Task: Narcissistic decimal number








VB: Private Function narcissistic(n As Long) As Boolean
    Dim d As String: d = CStr(n)
    Dim l As Integer: l = Len(d)
    Dim sumn As Long: sumn = 0
    For i = 1 To l
        sumn = sumn + (Mid(d, i, 1) - "0") ^ l
    Next i
    narcissistic = sumn = n
End Function

Public Sub main()
    Dim s(24) As String
    Dim n As Long: n = 0
    Dim found As Integer: found = 0
    Do While found < 25
        If narcissistic(n) Then
            s(found) = CStr(n)
            found = found + 1
        End If
        n = n + 1
    Loop
    Debug.Print Join(s, ", ")
End Sub


COBOL:        PROGRAM-ID. NARCISSIST-NUMS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
 
           01 num-length PIC 9(2) value 0.
           01 in-sum PIC  9(9) value 0.
           01 counter PIC  9(9) value 0.
           01 current-number PIC  9(9) value 0.
           01 narcissist PIC Z(9).
           01 temp PIC  9(9) value 0.
           01 modulo PIC  9(9) value 0.
           01 answer PIC  9 .
 
       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "the first 20 narcissist numbers:" .
 
           MOVE 20 TO counter.
           PERFORM UNTIL counter=0
 
               PERFORM 000-NARCISSIST-PARA
 
                   IF answer = 1 
                       SUBTRACT 1 from counter
                       GIVING counter
                       MOVE current-number TO narcissist
                       DISPLAY narcissist
                   END-IF
 
                   ADD 1 TO current-number
 
               END-PERFORM
 
            STOP RUN.
 
       000-NARCISSIST-PARA.
 
             MOVE ZERO TO in-sum.
             MOVE current-number TO temp.
             COMPUTE num-length =1+  FUNCTION Log10(temp)
 
             PERFORM  UNTIL temp=0
 
                  DIVIDE temp BY 10 GIVING temp
                            REMAINDER  modulo
 
                  COMPUTE modulo=modulo**num-length
                  ADD modulo to in-sum GIVING in-sum
 
            END-PERFORM.
 
               IF current-number=in-sum
                   MOVE 1 TO answer
                   ELSE MOVE 0 TO answer
               END-IF.
 
       END PROGRAM NARCISSIST-NUMS.
Task: Narcissistic decimal number




Go: package main

import "fmt"

func narc(n int) []int {
	power := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	limit := 10
	result := make([]int, 0, n)
	for x := 0; len(result) < n; x++ {
		if x >= limit {
			for i := range power {
				power[i] *= i 
			}
			limit *= 10
		}
		sum := 0
		for xx := x; xx > 0; xx /= 10 {
			sum += power[xx%10]
		}
		if sum == x {
			result = append(result, x)
		}
	}
	return result
}

func main() {
	fmt.Println(narc(25))
}






COBOL:        PROGRAM-ID. NARCISSIST-NUMS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
 
           01 num-length PIC 9(2) value 0.
           01 in-sum PIC  9(9) value 0.
           01 counter PIC  9(9) value 0.
           01 current-number PIC  9(9) value 0.
           01 narcissist PIC Z(9).
           01 temp PIC  9(9) value 0.
           01 modulo PIC  9(9) value 0.
           01 answer PIC  9 .
 
       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "the first 20 narcissist numbers:" .
 
           MOVE 20 TO counter.
           PERFORM UNTIL counter=0
 
               PERFORM 000-NARCISSIST-PARA
 
                   IF answer = 1 
                       SUBTRACT 1 from counter
                       GIVING counter
                       MOVE current-number TO narcissist
                       DISPLAY narcissist
                   END-IF
 
                   ADD 1 TO current-number
 
               END-PERFORM
 
            STOP RUN.
 
       000-NARCISSIST-PARA.
 
             MOVE ZERO TO in-sum.
             MOVE current-number TO temp.
             COMPUTE num-length =1+  FUNCTION Log10(temp)
 
             PERFORM  UNTIL temp=0
 
                  DIVIDE temp BY 10 GIVING temp
                            REMAINDER  modulo
 
                  COMPUTE modulo=modulo**num-length
                  ADD modulo to in-sum GIVING in-sum
 
            END-PERFORM.
 
               IF current-number=in-sum
                   MOVE 1 TO answer
                   ELSE MOVE 0 TO answer
               END-IF.
 
       END PROGRAM NARCISSIST-NUMS.
Task: Strip whitespace from a string_Top and tail



C#: using System;

public class TrimExample
{
    public static void Main(String[] args)
    {
        const string toTrim = " Trim me ";
        Console.WriteLine(Wrap(toTrim.TrimStart()));
        Console.WriteLine(Wrap(toTrim.TrimEnd()));
        Console.WriteLine(Wrap(toTrim.Trim()));
    }

    private static string Wrap(string s)
    {
        return "'" + s + "'";
    }
}









Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure StripDemo is
   str : String := "     Jabberwocky     ";
begin
   Put_Line ("'" & Trim (str, Left) & "'");
   Put_Line ("'" & Trim (str, Right) & "'");
   Put_Line ("'" & Trim (str, Both) & "'");
end StripDemo;
Task: Strip whitespace from a string_Top and tail

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char *rtrim(const char *s)
{
  while( isspace(*s) || !isprint(*s) ) ++s;
  return strdup(s);
}

char *ltrim(const char *s)
{
  char *r = strdup(s);
  if (r != NULL)
  {
    char *fr = r + strlen(s) - 1;
    while( (isspace(*fr) || !isprint(*fr) || *fr == 0) && fr >= r) --fr;
    *++fr = 0;
  }
  return r;
}

char *trim(const char *s)
{
  char *r = rtrim(s);
  char *f = ltrim(r);
  free(r);
  return f;
}

const char *a = "     this is a string      ";

int main()
{
  char *b = rtrim(a);
  char *c = ltrim(a);
  char *d = trim(a);

  printf("'%s'\n'%s'\n'%s'\n", b, c, d);
  
  free(b);
  free(c);
  free(d);
  return 0;
}











Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure StripDemo is
   str : String := "     Jabberwocky     ";
begin
   Put_Line ("'" & Trim (str, Left) & "'");
   Put_Line ("'" & Trim (str, Right) & "'");
   Put_Line ("'" & Trim (str, Both) & "'");
end StripDemo;
Task: Strip whitespace from a string_Top and tail


C++: #include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>

int main( ) {
   std::string testphrase( "    There are unwanted blanks here!    " ) ;
   std::string lefttrimmed = boost::trim_left_copy( testphrase ) ;
   std::string righttrimmed = boost::trim_right_copy( testphrase ) ;
   std::cout << "The test phrase is :" << testphrase << "\n" ;
   std::cout << "Trimmed on the left side :" << lefttrimmed << "\n" ;
   std::cout << "Trimmed on the right side :" << righttrimmed << "\n" ;
   boost::trim( testphrase ) ;
   std::cout << "Trimmed on both sides :" <<  testphrase  << "\n" ;
   return 0 ;
}










Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure StripDemo is
   str : String := "     Jabberwocky     ";
begin
   Put_Line ("'" & Trim (str, Left) & "'");
   Put_Line ("'" & Trim (str, Right) & "'");
   Put_Line ("'" & Trim (str, Both) & "'");
end StripDemo;
Task: Strip whitespace from a string_Top and tail




Go: package main

import (
    "fmt"
    "strings"
    "unicode"
)

var simple = `
    simple   `

func main() {
    show("original", simple)
    show("leading ws removed", strings.TrimLeftFunc(simple, unicode.IsSpace))
    show("trailing ws removed", strings.TrimRightFunc(simple, unicode.IsSpace))
    
    show("both removed", strings.TrimSpace(simple))
}

func show(label, str string) {
    fmt.Printf("%s: |%s| %v\n", label, str, []rune(str))
}








Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure StripDemo is
   str : String := "     Jabberwocky     ";
begin
   Put_Line ("'" & Trim (str, Left) & "'");
   Put_Line ("'" & Trim (str, Right) & "'");
   Put_Line ("'" & Trim (str, Both) & "'");
end StripDemo;
Task: Strip whitespace from a string_Top and tail





Java: public class Trims{
    public static String ltrim(String s) {
        int i = 0;
        while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
            i++;
        }
        return s.substring(i);
    }

    public static String rtrim(String s) {
        int i = s.length() - 1;
        while (i > 0 && Character.isWhitespace(s.charAt(i))) {
            i--;
        }
        return s.substring(0, i + 1);
    }

    public static String trim(String s) {
    	return rtrim(ltrim(s));
    }

    public static void main(String[] args) {
        String s = " \t \r \n String with spaces \u2009 \t  \r  \n  ";
        System.out.printf("[%s]\n", ltrim(s));
        System.out.printf("[%s]\n", rtrim(s));
        System.out.printf("[%s]\n", trim(s));
    }
}







Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure StripDemo is
   str : String := "     Jabberwocky     ";
begin
   Put_Line ("'" & Trim (str, Left) & "'");
   Put_Line ("'" & Trim (str, Right) & "'");
   Put_Line ("'" & Trim (str, Both) & "'");
end StripDemo;
Task: Strip whitespace from a string_Top and tail







Python: >>> s = ' \t \r \n String with spaces  \t  \r  \n  '
>>> s
' \t \r \n String with spaces  \t  \r  \n  '
>>> s.lstrip()
'String with spaces  \t  \r  \n  '
>>> s.rstrip()
' \t \r \n String with spaces'
>>> s.strip()
'String with spaces'
>>>





Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure StripDemo is
   str : String := "     Jabberwocky     ";
begin
   Put_Line ("'" & Trim (str, Left) & "'");
   Put_Line ("'" & Trim (str, Right) & "'");
   Put_Line ("'" & Trim (str, Both) & "'");
end StripDemo;
Task: Strip whitespace from a string_Top and tail








VB: Public Sub test()
    
    
    
    s = " trim "
    Debug.Print """" & s & """"
    Debug.Print """" & LTrim(s) & """"
    Debug.Print """" & RTrim(s) & """"
    Debug.Print """" & WorksheetFunction.trim(s) & """"
    
End Sub




Ada: with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure StripDemo is
   str : String := "     Jabberwocky     ";
begin
   Put_Line ("'" & Trim (str, Left) & "'");
   Put_Line ("'" & Trim (str, Right) & "'");
   Put_Line ("'" & Trim (str, Both) & "'");
end StripDemo;
Task: Strip whitespace from a string_Top and tail

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

char *rtrim(const char *s)
{
  while( isspace(*s) || !isprint(*s) ) ++s;
  return strdup(s);
}

char *ltrim(const char *s)
{
  char *r = strdup(s);
  if (r != NULL)
  {
    char *fr = r + strlen(s) - 1;
    while( (isspace(*fr) || !isprint(*fr) || *fr == 0) && fr >= r) --fr;
    *++fr = 0;
  }
  return r;
}

char *trim(const char *s)
{
  char *r = rtrim(s);
  char *f = ltrim(r);
  free(r);
  return f;
}

const char *a = "     this is a string      ";

int main()
{
  char *b = rtrim(a);
  char *c = ltrim(a);
  char *d = trim(a);

  printf("'%s'\n'%s'\n'%s'\n", b, c, d);
  
  free(b);
  free(c);
  free(d);
  return 0;
}
Task: Strip whitespace from a string_Top and tail



C#: using System;

public class TrimExample
{
    public static void Main(String[] args)
    {
        const string toTrim = " Trim me ";
        Console.WriteLine(Wrap(toTrim.TrimStart()));
        Console.WriteLine(Wrap(toTrim.TrimEnd()));
        Console.WriteLine(Wrap(toTrim.Trim()));
    }

    private static string Wrap(string s)
    {
        return "'" + s + "'";
    }
}
Task: Strip whitespace from a string_Top and tail


C++: #include <boost/algorithm/string.hpp>
#include <string>
#include <iostream>

int main( ) {
   std::string testphrase( "    There are unwanted blanks here!    " ) ;
   std::string lefttrimmed = boost::trim_left_copy( testphrase ) ;
   std::string righttrimmed = boost::trim_right_copy( testphrase ) ;
   std::cout << "The test phrase is :" << testphrase << "\n" ;
   std::cout << "Trimmed on the left side :" << lefttrimmed << "\n" ;
   std::cout << "Trimmed on the right side :" << righttrimmed << "\n" ;
   boost::trim( testphrase ) ;
   std::cout << "Trimmed on both sides :" <<  testphrase  << "\n" ;
   return 0 ;
}
Task: Strip whitespace from a string_Top and tail





Java: public class Trims{
    public static String ltrim(String s) {
        int i = 0;
        while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
            i++;
        }
        return s.substring(i);
    }

    public static String rtrim(String s) {
        int i = s.length() - 1;
        while (i > 0 && Character.isWhitespace(s.charAt(i))) {
            i--;
        }
        return s.substring(0, i + 1);
    }

    public static String trim(String s) {
    	return rtrim(ltrim(s));
    }

    public static void main(String[] args) {
        String s = " \t \r \n String with spaces \u2009 \t  \r  \n  ";
        System.out.printf("[%s]\n", ltrim(s));
        System.out.printf("[%s]\n", rtrim(s));
        System.out.printf("[%s]\n", trim(s));
    }
}
Task: Strip whitespace from a string_Top and tail







Python: >>> s = ' \t \r \n String with spaces  \t  \r  \n  '
>>> s
' \t \r \n String with spaces  \t  \r  \n  '
>>> s.lstrip()
'String with spaces  \t  \r  \n  '
>>> s.rstrip()
' \t \r \n String with spaces'
>>> s.strip()
'String with spaces'
>>>
Task: Strip whitespace from a string_Top and tail








VB: Public Sub test()
    
    
    
    s = " trim "
    Debug.Print """" & s & """"
    Debug.Print """" & LTrim(s) & """"
    Debug.Print """" & RTrim(s) & """"
    Debug.Print """" & WorksheetFunction.trim(s) & """"
    
End Sub
Task: Strip whitespace from a string_Top and tail




Go: package main

import (
    "fmt"
    "strings"
    "unicode"
)

var simple = `
    simple   `

func main() {
    show("original", simple)
    show("leading ws removed", strings.TrimLeftFunc(simple, unicode.IsSpace))
    show("trailing ws removed", strings.TrimRightFunc(simple, unicode.IsSpace))
    
    show("both removed", strings.TrimSpace(simple))
}

func show(label, str string) {
    fmt.Printf("%s: |%s| %v\n", label, str, []rune(str))
}