📌  相关文章
📜  将所有数字移动到给定字符串的开头

📅  最后修改于: 2021-09-04 09:36:21             🧑  作者: Mango

给定一个字符串S,任务是把所有的数字出现在字符串中,该字符串的开头。

例子:

做法:思路是遍历字符串,维护两个字符串,一个字符串包含数字,另一个字符串包含非数字字符。最后,按所需顺序将两个字符串附加到结果中。

下面是上述方法的实现:

Java
// Java program for the above approach
 
class GFG {
 
    // Function to move all the digit
    // to the beginning of the string
    static void moveAllDigitAtBeginning(
        String str)
    {
        // Calculate the string length
        int len = str.length();
 
        // Stores the digits
        StringBuilder digits
            = new StringBuilder();
 
        // Stores the non-numeric character
        StringBuilder nonNumericCharacter
            = new StringBuilder();
 
        // Traverse the string and
        // check if there is a digit
        for (char c : str.toCharArray()) {
 
            // If character is a digit,
            // add it to the string digits
            if (c >= 48 && c <= 57) {
                digits.append(c);
            }
 
            // Otherwise, add it to the
            // string nonNumericCharacter
            else {
                nonNumericCharacter.append(c);
            }
        }
 
        // Append both the strings
        digits.append(
            nonNumericCharacter.toString());
 
        // Print the string
        System.out.print(
            digits.toString() + " ");
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Given String str
        String str
            = "GeeksforGeeks123";
        moveAllDigitAtBeginning(str);
    }
}


Javascript


Java
// Java program for the above approach
 
class GFG {
 
    // Function to move all the digit
    // at the beginning of the string
    static void moveAllDigitAtBeginning(
        String str)
    {
        // Replace all the non-numeric
        // characters with " " and
        // replace all digits with " "
        String moveAllDigit = str.replaceAll("\\D+", "")
                              + str.replaceAll("\\d+", "");
 
        // Print the string
        System.out.println(moveAllDigit);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Given String str
        String str
            = "GeeksforGeeks1234";
        moveAllDigitAtBeginning(str);
    }
}


C#
// C# program for the above approach
using System;
using System.Text.RegularExpressions;
public class GFG
{
 
    // Function to move all the digit
    // at the beginning of the string
    static void moveAllDigitAtBeginning(
        String str)
    {
       
        // Replace all the non-numeric
        // characters with " " and
        // replace all digits with " "
        String moveAllDigit = Regex.Replace(str, "\\D+", "")
            +Regex.Replace(str, "\\d", "");
       
        // Print the string
        Console.WriteLine(moveAllDigit);
    }
 
    // Driver Code
    public static void Main(String []args)
    {
       
        // Given String str
        String str
            = "GeeksforGeeks1234";
        moveAllDigitAtBeginning(str);
    }
}
 
// This code is contributed by 29AjayKumar


输出:
123GeeksforGeeks

时间复杂度: O(N)
辅助空间: O(1)

基于正则表达式的方法:也可以使用正则表达式解决给定的问题,用空字符串(“”)替换所有非数字字符,给你所有数字,用空字符串(“”)替换所有数字,给你所有非数字字符。最后,连接字符串并打印结果。

下面是上述方法的实现:

Java

// Java program for the above approach
 
class GFG {
 
    // Function to move all the digit
    // at the beginning of the string
    static void moveAllDigitAtBeginning(
        String str)
    {
        // Replace all the non-numeric
        // characters with " " and
        // replace all digits with " "
        String moveAllDigit = str.replaceAll("\\D+", "")
                              + str.replaceAll("\\d+", "");
 
        // Print the string
        System.out.println(moveAllDigit);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Given String str
        String str
            = "GeeksforGeeks1234";
        moveAllDigitAtBeginning(str);
    }
}

C#

// C# program for the above approach
using System;
using System.Text.RegularExpressions;
public class GFG
{
 
    // Function to move all the digit
    // at the beginning of the string
    static void moveAllDigitAtBeginning(
        String str)
    {
       
        // Replace all the non-numeric
        // characters with " " and
        // replace all digits with " "
        String moveAllDigit = Regex.Replace(str, "\\D+", "")
            +Regex.Replace(str, "\\d", "");
       
        // Print the string
        Console.WriteLine(moveAllDigit);
    }
 
    // Driver Code
    public static void Main(String []args)
    {
       
        // Given String str
        String str
            = "GeeksforGeeks1234";
        moveAllDigitAtBeginning(str);
    }
}
 
// This code is contributed by 29AjayKumar
输出:
1234GeeksforGeeks

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live